Tutorials with Arduino: Range Sensor with LCD and 7 segment led #display #arduino

It finally arrived! the Range Sensor we bought from Ebay! it´s time to see it working with our Arduino UNO board.

It´s the Ultrasonic Ranging Module HC-SR04, who works as a sonar, sending and ultrasonic sound and measuring how long does it takes to come back. This delay it´s transformed in cm by calculating the speed of the sound and the time it took to come back.

It´s a very simple easy to use sensor, that can have many applications from robots, to alarms, or even home appliances… even as a car parking assistant!

The sensor has 4 pins: 2 for +VCC and Ground and 2 for the data, this 2 goes to 2 of the Arduino digital ports

To see the measurement of the range we have plenty of options: from seeing it in the Serial Print, an LCD display, 7 segment leds, on twitter, or anything you want. We will use the LCD and the 7 segment led display for this.

See it in action:

You can download the library here.




Code for LDC Display:

#include "Ultrasonic.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Ultrasonic ultrasonic(7,8);

void setup() {
lcd.begin(16, 2);

void loop()
  lcd.setCursor(0, 0);
  lcd.setCursor(0, 1);
  lcd.print("Distance: ");

Code for 7 segment led display:

Made by @mrlndr for Arduinoarts.com, 
based on the 4 character led display thermometer example.


#include "Ultrasonic.h"
//#include <LiquidCrystal.h>
//LiquidCrystal lcd(11, 10, 9, 4, 5, 6, 7);
Ultrasonic ultrasonic(12,11);

int clockPin = 7;  //Pin connected to SH_CP 
int dataPin = 6;  //Pin connected to DS 
int digit1Pin = 2;  
int digit2Pin = 3;
int digit3Pin = 4;
int digit4Pin = 5;

byte data;
byte dataArray[13];

int val=0;
int val1;
int val2;
int val3;
int val4;

void setup(){
  pinMode(digit1Pin, OUTPUT);
  pinMode(digit2Pin, OUTPUT);
  pinMode(digit3Pin, OUTPUT);
  pinMode(digit4Pin, OUTPUT);

  //pinMode(latchPin, OUTPUT);


  dataArray[0] = B11000000;
  dataArray[1] = B11111001;
  dataArray[2] = B10100100;
  dataArray[3] = B10110000;
  dataArray[4] = B10011001;
  dataArray[5] = B10010010;
  dataArray[6] = B10000010;
  dataArray[7] = B11111000;
  dataArray[8] = B10000000;
  dataArray[9] = B10010000;


void loop(){
  val = ultrasonic.Ranging(CM);
  Serial.print("Val = ");
  Serial.println(val);     // reading the values
  if (val<100){
  val1 = val/1000;
  val2 = (val-(val1*1000))/100;
  val3 = (val-((val1*1000) + (val2*100)))/10;
  val4 = (val-((val1*1000) + (val2*100) + (val3*10)));
  else {
  val1 = 0;
  val2 = 5;
  val3 = 0;
  val4 = 0;
  Serial.print("Val4 = ");
   Serial.println(val4);     // reading the values

  setDigit(digit1Pin, val1);
  setDigit(digit2Pin, val2);
  setDigit(digit3Pin, val3);
  setDigit(digit4Pin, val4);

void setDigit(int digitPin, int value){
    //digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, dataArray[value]);  
    //digitalWrite(latchPin, 1);
    digitalWrite(digitPin, HIGH);
    digitalWrite(digitPin, LOW);  

void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first,
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
	pinState= 1;
    else {
	pinState= 0;

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin  
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);

  //stop shifting
  digitalWrite(myClockPin, 0);



Related Posts

  • Last summer, my girlfriend and me got a beautifull dog named "Luna" (moon in spanish), a white Golden Retriever (you can see some videos on her youtube channel here)  it´s been amazingly fun but has a problem... the fur... the hair... the dirt on the house. This is Luna: I…
  • Finally i did it! i was with the idea of this project for so long... and finally got time to do it. I know is a very common project on the web, many sites with the videos and tutorials, but is so cool that was worth the time! (and once…
  • It´s Summer time!!!! (it means we have some crazy hot days in Madrid... 40º C!!!!) This summer has been quite complicated in Spain, and in my flat i don´t have air conditioning, so i needed to have a solution fast, since the fan was not doing enough... i came up…