3.1.3 Reversing Alarm

Introduction

In this project, we will use LCD, buzzer and ultrasonic sensors to make a reverse assist system. We can put it on the remote control vehicle to simulate the actual process of reversing the car into the garage.

Required Components

In this project, we need the following components.

../_images/list_Reversing_Alarm.png

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Raphael Kit

337

Raphael Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

GPIO Extension Board

BUY

Breadboard

BUY

Jumper Wires

BUY

Resistor

BUY

Buzzer

-

Transistor

BUY

Ultrasonic Module

BUY

I2C LCD1602

BUY

Schematic Diagram

Ultrasonic sensor detects the distance between itself and the obstacle that will be displayed on the LCD in the form of code. At the same time, the ultrasonic sensor let the buzzer issue prompt sound of different frequency according to different distance value.

T-Board Name

physical

wiringPi

BCM

GPIO23

Pin 16

4

23

GPIO24

Pin 18

5

24

GPIO17

Pin 11

0

17

SDA1

Pin 3

SCL1

Pin 5

../_images/Schematic_three_one3.png

Experimental Procedures

Step 1: Build the circuit.

../_images/image242.png

Step 2: Change directory.

cd ~/raphael-kit/c/3.1.3/

Step 3: Compile.

gcc 3.1.3_ReversingAlarm.c -lwiringPi

Step 4: Run.

sudo ./a.out

As the code runs, ultrasonic sensor module detects the distance to the obstacle and then displays the information about the distance on LCD1602; besides, buzzer emits warning tone whose frequency changes with the distance.

Note

  • If there is an error prompt wiringPi.h: No such file or directory, please refer to Install and Check the WiringPi.

  • If you get Unable to open I2C device: No such file or directory error, you need to refer to I2C Configuration to enable I2C and check if the wiring is correct.

  • If the code and wiring are fine, but the LCD still does not display content, you can turn the potentiometer on the back to increase the contrast.

Code

Note

The following codes are incomplete. If you want to check the complete codes, you are suggested to use command nano 3.1.1_ReversingAlarm.c.

#include <wiringPi.h>
#include <stdio.h>
#include <sys/time.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <string.h>

#define Trig    4
#define Echo    5
#define Buzzer  0

int LCDAddr = 0x27;
int BLEN = 1;
int fd;

//here is the function of LCD
void write_word(int data){...}

void send_command(int comm){...}

void send_data(int data){...}

void lcdInit(){...}

void clear(){...}

void write(int x, int y, char data[]){...}

//here is the function of Ultrasonic
void ultraInit(void){...}

float disMeasure(void){...}

//here is the main function
int main(void)
{
    float dis;
    char result[10];
    if(wiringPiSetup() == -1){
        printf("setup wiringPi failed !");
        return 1;
    }

    pinMode(Buzzer,OUTPUT);
    fd = wiringPiI2CSetup(LCDAddr);
    lcdInit();
    ultraInit();

    clear();
    write(0, 0, "Ultrasonic Starting");
    write(1, 1, "By Sunfounder");

    while(1){
        dis = disMeasure();
        printf("%.2f cm \n",dis);
        digitalWrite(Buzzer,LOW);
        if (dis > 400){
            clear();
            write(0, 0, "Error");
            write(3, 1, "Out of range");
            delay(500);
        }
        else
        {
            clear();
            write(0, 0, "Distance is");
            sprintf(result,"%.2f cm",dis);
            write(5, 1, result);

            if(dis>=50)
            {delay(500);}
            else if(dis<50 & dis>20) {
                for(int i=0;i<2;i++){
                digitalWrite(Buzzer,HIGH);
                delay(50);
                digitalWrite(Buzzer,LOW);
                delay(200);
                }
            }
            else if(dis<=20){
                for(int i=0;i<5;i++){
                digitalWrite(Buzzer,HIGH);
                delay(50);
                digitalWrite(Buzzer,LOW);
                delay(50);
                }
            }
        }
    }

    return 0;
}

Code Explanation

pinMode(Buzzer,OUTPUT);
fd = wiringPiI2CSetup(LCDAddr);
lcdInit();
ultraInit();

In this program, we apply previous components synthetically. Here we use buzzers, LCD and ultrasonic. We can initialize them the same way as we did before.

dis = disMeasure();
 printf("%.2f cm \n",dis);
digitalWrite(Buzzer,LOW);
if (dis > 400){
     write(0, 0, "Error");
     write(3, 1, "Out of range");
}
else
{
    write(0, 0, "Distance is");
    sprintf(result,"%.2f cm",dis);
    write(5, 1, result);
    }

Here we get the value of the ultrasonic sensor and get the distance through calculation.

If the value of distance is greater than the range value to be detected, an error message is printed on the LCD. And if the distance value is within the range, the corresponding results will be output.

sprintf(result,"%.2f cm",dis);

Since the output mode of LCD only supports character type, and the variable dis stores the value of float type, we need to use sprintf(). The function converts the float type value to a character and stores it on the string variable result[]. %.2f means to keep two decimal places.

if(dis>=50)
{delay(500);}
else if(dis<50 & dis>20) {
    for(int i=0;i<2;i++){
    digitalWrite(Buzzer,HIGH);
    delay(50);
    digitalWrite(Buzzer,LOW);
    delay(200);
    }
}
else if(dis<=20){
    for(int i=0;i<5;i++){
    digitalWrite(Buzzer,HIGH);
    delay(50);
    digitalWrite(Buzzer,LOW);
    delay(50);
    }
}

This judgment condition is used to control the sound of the buzzer. According to the difference in distance, it can be divided into three cases, in which there will be different sound frequencies. Since the total value of delay is 500, all of the cases can provide a 500ms interval for the ultrasonic sensor.

Phenomenon Picture

../_images/image243.jpeg