Filters
Close

Frobo V2 Programming Lesson- Part 4

So far, we learnt uisning Leds as output and how to blink them in different patterns. Let us know try to learn how to read an input from a sensor. Frobo V2 kit comes with 3 different kinds of input sensors. 

  1. HC-SR04 ultrasonic sensor. This works on the principle of sound echo. Detailed discussion below in this Part only.
  2. CDS- LDR Photo Sensor is a light sensitive resistive sensor, i .e. the sensor changes it's electrical resistance when brightness of light falling on it changes. We measure the resistance change and calvcualte the light brightness. We will learn more of it lesson- Part 6.
  3. IR Photocouple Sensor is a special sensor to measure reflected infrared to measure distance from a nearby object. This sensor is used in many applications like automobiles and industrial applications. We will learn more on this sensor is lesson Part 5.

This lesson will teach you about using ultrasonic sensor to calcualte distance of Frobo V3 from an object in front. Let us first try to understand what is ultrasonic and how it is different from other sounds.

DO YOU KNOW WHY YOU DON'T HEAR ULTRASONIC?

Sound travels like a wave. A wave is defined as a progression of energy from one point to another through a medium without transporting the medium. A wave travels by giving energy to the neighboring particles of the medium. These particles start oscillating and further transfer energy to the next particle. The waves propagate through the medium with a specific frequency, wavelength and amplitude. Wave length is inversely proportional to frequency, i.e. higher the frequency, lower the wave length. The amplitude refers to the maximum extent of oscillation. Please see the diagram below to understand the difference. Human ear is designed to hear sound frequencies between 20 Hz (Sound with wave frequency which goes 20 times up and down in a second) and 20,000 Hz or 20 KHz (Kilo Hertz). Any sound frequency below or above is heard by normal huma ear. So, the sound frequencies between 20 Hz to 20 KHz are know as SONIC waves. Below 20 Hz are called INFRASONIC and above 20KHz are know as ULTRASONIC.

You must read more about sound. Here is a useful link for some important details.

 What is HC-SR04 Ultrasonic Sensor?

Frobo V3 HC-SR04 Ultrasonic Sensor   

 Do you know how sound echoes? Have you ever noticed in an empty room, your voice repeats. This happens because sound waves travel through air and when the oscillating air particles collide with a solid object, it bounces them back, much in the same way as a ball bounces back. When the room is empty, these reflected sound waves reach your ears like repeated sounds. Our sensor HC-SR04 uses the same principle. It emits very high frequency (40 KHz) ultrasonic from its frequency generator and when these ultrasonic waves collide with an object (within 4 meters), it creates ultrasonic echo. The sensor has an ultrasonic echo transducer which converts these reflected waves to electric voltage. This echo volt is then amplified through an onboard amplifier. Our froboduino pin 24 (Arduino programming pin A1) receives this echo voltage as a signal. We record the time difference between when ultrasonic sound was generated and when echo was received to calculate how much time passed before echoes were received. We know the spped of  sound in air 343meters/second. So we can calculate how far the object is. You will learn more about distance calculation below when we explain the program to read this sensor.

This 4 pin easy to use sensor is very popular in robotic applications. Two pins are used to power the device with 5V and ground and the other two pins make the device work. One pin is called "Trig" or "Trigger". This pin is set to HIGH voltage by MCU and it starts emitting ultrasonic sound waves. The other pin is called "Echo". This pin sends data to the MCU when it receives a reflected ultrasonic echo after colliding with an object. This sensor has a detection range of approximatley 2 cms to 4 meters. Beyond 4 meters the reflected sound intensity drops below capacity of this sensor.

Please check datasheet of the sensor here. The datasheet carries all technical specifiactions for the sensor by the manufacturer.

Now, let us look at the program to read the sensor distance measurement program.

 

// define pins numbers for "Trig" and "Echo" Pins of HC-SR04.

//Our Froboduino Ultrasonic (U) ports are connected to Arduino pin D2 and D3.

const int trigPin = 2;

const int echoPin = 3;

// defines variables. 

long duration;

int distance;

 

void setup() {

// Sets the trigPin as an Output

pinMode(trigPin, OUTPUT);

 // Sets the echoPin as an Input

 pinMode(echoPin, INPUT);

// Starts the serial communication

 Serial.begin(9600); }

  void loop() {

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

 // Sets the trigPin on HIGH state for 5 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(5);

digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);

// Calculate the distance. Sound travel at 343 M/s. So distance =durationX343 Meters/second = durationX34300 cms/ 1000*1000 microseconds = duration*.0343

// Divide the total distance travelled by sound by 2 as it goes and comes back to get distance from object.

distance= duration*0.0343/2;

// Prints the distance on the Serial Monitor . Serial monitor is not left top side of your Arduino IDE.

// Make sure to select right board and prt from tool menu.

Serial.print("Distance: ");

Serial.println(distance); }

Here is the Video for your reference

Try uploading this program on your arduino Uno and connect Ultrasonic to Arduino Pins "Trig" 14 (A0), "Echo" to 15 (A1) and %v to 5V and GND to GND and read the values on your serial monitor.

Now, that we can measure distance of an object, Can we make this data to actuate some "output" when distance reads below and above certain value. We already learnt about blinking Leds. Ok, Let's try the following-

  • When distance measured is below 10 cms . Light up left Led.
  • When distance measured is graeter than 10 but less than 1000 cms( 1 meter). Light up right Led.
  • When distance measured is greater than 1000 cms. Light up both Leds.
 

// define pins numbers for "Trig" and "Echo" Pins of HC-SR04.
//Our Froboduino Ultrasonic (U) ports are connected to arduino pin D2 and.
// Define left and right Leds pins to D9, D10
const int trigPin = 2;
const int echoPin = 3;
const int leftLed = 9;
const int rightLed = 10;

// defines variables. We are defining "duration" as "long" variable and "distance" as "int". Arduino compiler treats diffrent variable types differently.

// Read Here to understand different "Integer Constants"
long duration;
int distance;

void setup() {

// Set left Led as output.

pinMode(leftLed, OUTPUT);

// Set right Led as output.

pinMode(rightLed, OUTPUT);
// Sets the trigPin as an Output
pinMode(trigPin, OUTPUT);
// Sets the echoPin as an Input. It is not important to define inputs, but for clarity of this we are showing it heere.

pinMode(echoPin, INPUT);
// Starts the serial communication

Serial.begin(9600); }

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 5 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance. Sound travel at 343 M/s. So distance =durationX343 Meters/second = durationX34300 cms/ 1000*1000 microseconds = duration*.0343
// Divide the total distance travelled by sound by 2 as it goes and comes back to get distance from object.
distance= duration*0.0343/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
//Code for When distance measured is below 10 cms . Light up left Led. Turn off right led.

if (distance <= 10){
//Light up left Led
digitalWrite(leftLed, HIGH);
digitalWrite(rightLed, LOW);
}
//Code for When distance measured is above 10 cms but less than 1000 cms . Light up right Led. Turn off left Led.
if (distance >= 10 && distance <=1000){
//light up right Led
digitalWrite(rightLed, HIGH);
digitalWrite(leftLed, LOW);
}
//Code for When distance measured is above 1000 cms and less than 3000 cms . Light up left and right Led.
if (distance >= 1000 && distance <= 3000){
//light up left and right Led
digitalWrite(rightLed, HIGH);
digitalWrite(leftLed, HIGH);
}

}

Try uploading tghis code to your arduino/Froboduino and see ig leds light up as planned. Aren't we learning something exciting. Try more patterns by blinking leds rapidly or slowly when distance changes within a range. Experiment with different ranges.

We will learn about using CDS LDR Photo sensors in lesson Part 4.

Leave your comment