This week we are introduced to ultrasonic module.

Connecting the Ultrasonic module pins/ wire on to arduino board:

  • Black (Ultrasonic module) – Gnd > Gnd (Arduino board)
  • Yellow (Ultrasonic module) – +5V > Vcc (Arduino board)
  • Blue (Ultrasonic module) – Trigger > D8 (Arduino board)
  • Red ( Ultrasonic module) – Echo > D7 (Arduino board)

Example Code for the Ultrasonic module which was given to us as reference:

#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);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

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){
/* Send a negative number to computer and Turn LED ON
to indicate “out of range” */
Serial.println(“-1”);
digitalWrite(LEDPin, HIGH);

}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.println(distance);
digitalWrite(LEDPin, LOW);
}
//Delay 100ms before next reading.
delay(100);
}


By using the code above we can incorporate it with the LCD screen code which can display the distance/reading being recorded by the ultrasonic module here is my code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

#define echoPin 7
#define trigPin 8
#define LEDPin 13

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);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
lcd.begin(20,4);
}

void loop(){

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

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

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

distance = duration/58.2;

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

lcd.clear();
lcd.setCursor(0,0);
lcd.print(“-1”);
digitalWrite(LEDPin, HIGH);

}
else {

lcd.clear();
lcd.setCursor(0,0);
lcd.print(distance);
digitalWrite(LEDPin, LOW);
}

}


The problem with this particular code is that when it come to display the reading of the ultrasonic module on to LCD screen. The reading are displayed as a continuous number or series of numbers making it very hard/ confusing to see. This is because I have define the position the reading to start at character o on line 0.


I found that this week was the most difficult in terms of coding compared to previous tasks. The main reason I found that this week was difficult was that I had trouble understanding the two codes which does separate thing and combining the two so that will work together.

Print Friendly, PDF & Email