Ultrasonic Sensor to display a message when motion is detected

Hi guys,

Relatively new to Arduino and C++ so please bear with me.

I am making a project where I display a message on an LCD screen when motion is detected on an ultrasonic sensor. I can get a message on the screen, but not to work together with my ultrasonic sensor. Here is my code, any help would be greatly appreciated.

#include <LiquidCrystal.h>

LiquidCrystal lcd(6, 7, 5, 4, 3, 2);

const int pingPin = 10; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 11; // Echo Pin of Ultrasonic Sensor

void setup() {

lcd.begin(16, 2);


void loop() {
lcd.setCursor(2, 0);
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
digitalWrite(pingPin, HIGH);
digitalWrite(pingPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = (duration/2) / 74;
lcd.print(“PLease scan your card”);

int ping(int TriggerPin, int EchoPin) {
long duration, distanceCm;
digitalWrite(TriggerPin, LOW);
digitalWrite(TriggerPin, HIGH);
digitalWrite(TriggerPin, LOW);
duration = pulseIn(EchoPin, HIGH);
distanceCm = duration * 10 / 292/ 2;
if (distanceCm <= 20) {
digitalWrite(20, HIGH);

if (distanceCm >= 20) {

digitalWrite(13, LOW);
return distanceCm;


when motion is detected

Do you actually mean [u]motion[/u], or just [u]presence[/u]?

To say that there's [u]motion[/u], you would need to measure the distance to an object "now" and then again "in a while" and compare the two. If they differ, then there was motion.

But if you mean rather that you want to detect [u]presence[/u], you just need to compare the object's distance to some threshold distance.

Your code's quite a mess, I hate to say. You have code in loop() to read the sensor, as well as code in a function ping() which does much the same but is never called. And you have code at the bottom (the if (distanceCm >= 20) part) which is not in any function.

It actually looks as if you want to display a message "Please scan your card" (although that won't fit in 16 characters) and turn on the led if someone is closer than 20cm, is that correct?

Clarify what it is you want to do, and I'm sure we can help....

Your code can be:

#include <LiquidCrystal.h>

int trigPin = 9;    // TRIG pin
int echoPin = 8;    // ECHO pin

float duration_us, distance_cm;

// LCD pins <--> Arduino pins
const int RS = 11, EN = 12, D4 = 2, D5 = 3, D6 = 4, D7 = 5;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);

void setup() {
  // begin serial port
  Serial.begin (9600);
  lcd.begin(16, 2); // set up number of columns and rows

  // configure the trigger pin to output mode
  pinMode(trigPin, OUTPUT);
  // configure the echo pin to input mode
  pinMode(echoPin, INPUT);

void loop() {
  // generate 10-microsecond pulse to TRIG pin
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(echoPin, HIGH);

  // calculate the distance
  distance_cm = 0.017 * duration_us;

  // print the value to Serial Monitor
  Serial.print("distance: ");
  Serial.println(" cm");

  if (distanceCm >= 20) {
     lcd.print("PLease scan your card"); // print the char to the lcd
  else {

For detail instruction, see Arduino - LCD tutorial and Arduino - ultrasonic sensor tutorial