.. note:: Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts. **Why Join?** - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team. - **Learn & Share**: Exchange tips and tutorials to enhance your skills. - **Exclusive Previews**: Get early access to new product announcements and sneak peeks. - **Special Discounts**: Enjoy exclusive discounts on our newest products. - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions. 👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today! 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. Components ---------------- .. image:: img/list_Reversing_Alarm.png :align: center 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 ============ ======== ======== === .. image:: img/Schematic_three_one3.png :width: 800 :align: center Experimental Procedures ------------------------ **Step 1:** Build the circuit. .. image:: img/image242.png :width: 800 :align: center **Step 2:** Change directory. .. raw:: html .. code-block:: cd ~/davinci-kit-for-raspberry-pi/c/3.1.3/ **Step 3:** Compile. .. raw:: html .. code-block:: gcc 3.1.3_ReversingAlarm.c -lwiringPi **Step 4:** Run. .. raw:: html .. code-block:: 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 :ref:`install_wiringpi_pi5`. * If you get ``Unable to open I2C device: No such file or directory`` error, you need to refer to :ref:`i2c_config` 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``. .. code-block:: c #include #include #include #include #include #include #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); delay(100); 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** .. code-block:: c 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. .. code-block:: c 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. .. code-block:: c 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. .. code-block:: c 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.