Home Code HC-SR04 Ultrasonic Sensor and LCD example

HC-SR04 Ultrasonic Sensor and LCD example

by shedboy71

In this example we will connect an LCD keypad shield commonly used on Arduino projects and then connect an ultrasonic sensor to this shield. The ultrasonic sensor used is the same one used in the Nucleo and HC-SR04 Ultrasonic Sensor example. Here is the shield

lcd keypad shield

lcd keypad shield

Layout

This shows the LCD and ultrasonic sensor, the shield is fitted to the Arduino pins of your ST Nucleo

NUcleo and HCSR04 and LCD_bb

NUcleo and HCSR04 and LCD_bb

Code

You need to import a hcrs04 library and the freetronics lcd shield library for this project

[codesyntax lang=”cpp”]

#include "mbed.h"
#include "freetronicsLCDShield.h"
#include "hcsr04.h"
 
freetronicsLCDShield lcd(D8, D9, D4, D5, D6, D7, D1, A0);
//D12 TRIGGER D11 ECHO
HCSR04 sensor(D12, D11); 
//Serial pc(SERIAL_TX, SERIAL_RX); // Initialize UART connection
 
int main() 
{
    //lcd.cls();
    //lcd.printf("Distance");
    wait(1);
    
    int i=1;
    while (i++) 
    {
        lcd.cls();
        lcd.printf("Distance");
        long distance = sensor.distance();        
        lcd.setCursorPosition(1, 0);
        lcd.printf("%d cm",distance);
        wait(0.1);
    }
}

[/codesyntax]

 

Links

NUCLEO-F411RE STM32F411RE Core Board

LCD Keypad Shield LCD1602 LCD 1602 Module Display For Arduino

Share

You may also like