Arduino – Week 3 – Displays and Ultrasonic sensors

Arduino LCD Displays

Wire up the arduino using these following instructions

  • VCC (Red) to 5V
  • Ground/GND (Black) to GND
  • SDA (Blue) to A4
  • SCL (Yellow) to A5

The code used was downloaded adn the library installed in the specific location. Ensure that the LCD drivers are included in the same folder

#include <Wire.h>  // Comes with Arduino IDE
// Get the LCD I2C Library here: 
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads

#include <LiquidCrystal_I2C.h>

// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);  // Used to type in characters
  lcd.begin(20,4);         // initialize the lcd for 20 chars 4 lines, turn on backlight

  //-------- Write characters on the display ------------------
  // NOTE: Cursor Position: Lines and Characters start at 0  
  // lcd.setCursor(Horizontal position,Line) 
  
  lcd.setCursor(4,0); //Start at character 4 on line 0
  lcd.print("GO BACK");
  lcd.setCursor(4,1);  //Next start at character 6 on line 1
  lcd.print("TO");
  delay(1000);
  lcd.setCursor(4,2);
  lcd.print("THE");
  delay(1000);  
  lcd.setCursor(4,3);
  lcd.print("SHADOWS");
  
  delay(4000);
  // Wait and then tell user they can start the Serial Monitor and type in characters to
  // Display. (Set Serial Monitor option to "No Line Ending")
  lcd.setCursor(0,0); //Start at character 0 on line 0

  lcd.clear(); //Clears the screen.
  lcd.print("Start Serial Monitor");
  lcd.setCursor(0,1);
  lcd.print("Type characters");  
  lcd.setCursor(0,2);
  lcd.print("to display");  


}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  {
    // when characters arrive over the serial port...
    if (Serial.available()) {
      // wait a bit for the entire message to arrive
      delay(100);
      // clear the screen
      lcd.clear();
      // read all the available characters
      while (Serial.available() > 0) {
        // display each character to the LCD
        lcd.write(Serial.read());
      }
    }
  }

}/* --(end main loop )-- */



Displaying ultrasonic measurements on LCD screen

This is the code for running the screen and the ultrasound sensor all as the same time, displaying the range in cm to the LCD screen attached to the the arduino

It is a combination of both the ultrasonic sketch downloaded from student central and the above LCD sketch. The basics behind it is setting up the ultrasonic sensor as an input and keeping the LCD as an output

After some playing around to change the refresh rate (delay) of the LCD and where the stationary text was it looked good and could accurately read the distance if it was within its range

#include <Wire.h> // Comes with Arduino IDE
// Get the LCD I2C Library here:
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads

#include <LiquidCrystal_I2C.h>

// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration;
long distance; // Duration used to calculate distance

void setup() {
Serial.begin(9600);
lcd.begin(20,4);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT);

lcd.setCursor(0,0);
lcd.print("Range Finder");
lcd.setCursor(0,1);
lcd.print("Current Distance : ");
lcd.setCursor(7,2);
lcd.print("cm");

}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Measures the length of a pulse on echoPin in microseconds
//waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing.
//Returns the length of the pulse in microseconds.

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;

if (distance >= maximumRange || distance <= minimumRange){

lcd.clear();
lcd.setCursor(0,0);
lcd.print("Range Finder");
lcd.setCursor(0,1);
lcd.print("Current Distance : ");
lcd.setCursor(4,2);
lcd.print(distance);
lcd.setCursor(7,2);
lcd.print("cm");
delay(300);
digitalWrite(LEDPin, HIGH);

}
else {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Range Finder");
lcd.setCursor(0,1);
lcd.print("Current Distance : ");
lcd.setCursor(4,2);
lcd.print(distance);
lcd.setCursor(7,2);
lcd.print("cm");
delay(300);
digitalWrite(LEDPin, LOW);
}
delay(500);
}
Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*