Ultrasonic Sensor & InventOne

Introduction

Have you ever thought about creating a self-navigating car? Do you want to build an automatic pump system? We won’t be building any of the above here but we can give you a head-start. An ultrasonic sensor can help you achieve either of the previously mentioned projects all you need is to think out of the box. 

So let’s start, here we would be detecting the distance of an object from our current position.  

 

List of Components

Ultrasonic sensor

Breadboard

Jumper wires

InventOne board

 

You can get most of the components from any online store aliexpress, Hub360, amazon or ebay. Click here to order for your inventone board.

 

Tutorial

The circuit diagram is shown in the image below, we created this image using fritzing. Please ensure you take note of the pin numbers when making your connections as this would affect the functioning of your project, you could also remove the breadboard since you can connect the sensor directly to the inventone dev board.

Check out this great tutorial by Dejan Nedelkovski for more info on how an ultrasonic sensor work.

Circuit diagram for the distance sensing project
Circuit diagram for distance sensing project

 

Code

We’d jump straight to the loop section of the code if you have any issue with the other parts of the code, please see this tutorial which discusses the arduino sketch structure. In this code we trigger the sensor for approximately 1millisecond; during this period the ultrasonic sensor sends out a burst of sonic waves which gets reflected by the obstacle. Using the pulseIn( ) function, we can determine the time it takes for the echo to reach the sensor. This function turns on the echo pin which enables the ultrasonic sensor listen to the incoming echo from whatever obstacle was placed in the way of the waves we sent out at first. Doing very little calculations which you need not bother yourself about, we were able to get the distance of the obstacle from the sensor’s location.

Finally, using a couple of print( ) and println( ) functions we print the calculated distance with some added messages to the serial monitor. The delay is just to enable the sensor settle before trying to take another measurement.

 

/*This code senses the distance of an obastacle using the HC-SR04 ultrasonic sensor*/
int Trigger = 13;
int Echo = 12;
int distance, duration;
void setup() {
  pinMode(Trigger, OUTPUT);
  pinMode(Echo, INPUT);
  Serial.begin(9600);
}

void loop() {
  //Send sound wave to obstacle
  digitalWrite(Trigger, HIGH);
  delayMicroseconds(1000);
  //Stop sending the wave
  digitalWrite(Trigger, LOW);
  //Calculate distance from the obstacle using wave

  //reflection
  duration = pulseIn(Echo, HIGH);
  distance = (duration/2)/29.1;
  Serial.print("You are ");
  Serial.print(distance);
  Serial.println(" cm away from the obstacle");
  delay(500);
}

 

Back to the self-navigating car, to design this you can place about two ultrasonic sensors on the front and rear of the car, connect them up to the inventone dev board which has been programmed such that when the ultrasonic sensor senses an object at a particular distance from it, the car automatically turns; check out our Motor Driver tutorial on how to control a motor using the inventone board.

Have you had an idea on how to design an automatic pump system? I’d leave that to you to figure out for yourself.