Raspberry Pi Ultrasonic ranging + buzzer (C language)

Source: Internet
Author: User

We have explained in detail the Raspberry Pi control ultrasonic module ranging (http://www.cnblogs.com/yuemo/p/8888342.html) and ultrasonic control buzzer module audible (http://www.cnblogs.com/ yuemo/p/8906111.html).

Today we'll see how to apply all two modules and make an anti-theft device.

First look at the connection of each pin (requires seven mother-to-female DuPont line):

Ultrasonic module:

1.VCC: Connect 5V power supply (pin 1th)

2.Trig: Output port (terminal 16th pin)

3.Echo: Input port (port No. 18th)

4.Gnd: Ground End (Port 6th)

Buzzer module

1.GND grounding pin (Raspberry Pi 14th pin)

2.I/O input/Output pin (Raspberry Pi 11th pin--gpio.0)

3.VCC power supply Pin (Raspberry Pi 4th pin)

The following is the C language code:

#include <wiringPi.h> #include <stdio.h> #include <sys/time.h> #define TRIG 4#define Echo 5#define  VOICE 0void ultrainit (void) {Pinmode (echo,input);  Pinmode (Trig,output); Pinmode (voice,output);}  float dismeasure (void) {struct timeval tv1;      /* struct timeval{time_t tv_sec;    suseconds_t tv_usec;  } */struct timeval TV2;  Long start=0,stop=0;  Digitalwrite (Vdice,low);  Digitalwrite (Trig,low);  Delaymicroseconds (2);  Digitalwrite (Trig,high);  Delaymicroseconds (10);  Digitalwrite (Trig,low); while (!) (  Digitalread (Echo) = = 1));  Gettimeofday (&tv1,null); while (!) (  Digitalread (Echo) = = 0));  Gettimeofday (&tv2,null);  Start = tv1.tv_sec * 1000000 + tv1.tv_usec;  Stop = tv2.tv_sec * 1000000 + tv2.tv_usec; return (float) (stop-start)/1000000*34000/2;}    void DoVoice (void) {for (int i=1;i<=6;i++) {digitalwrite (Voice,high);    Delay (500);    Digitalwrite (Voice,low);  Delay (1000);  }}int Main () {float dis;   float olddis=0; if (wiringpisetup () ==-1) {printf ("Setup Wiringpi failed!");  return 1;  } ultrainit ();    while (1) {dis = Dismeasure ();     printf ("dismeasure =%0.2f cm\n", dis);       if (olddis==0) {Olddis=dis;     Continue       }else{int S=dis-olddis; if (s<-30| |       s>30) {doVoice ();     } Olddis=dis;  } delay (1000); } return 0;}

Our idea of this program for the use of ultrasonic ranging, recording the distance of the last ultrasonic ranging, and the distance between the ultrasonic range comparison, if the distance between 30cm or more, then buzzer alarm. Thought and programming are relatively simple.

If you find that some functions are not clear in the process of reading the program, please visit the blog: https://www.cnblogs.com/lulipro/p/5992172.html

    This experiment was first tried in a new field and has never previously been approached by Raspberry Pi based program development. At first, there were a lot of problems:    1. The Raspberry Pi system burned incorrectly, and it was a long time.    2. The choice of laboratory equipment, for me to choose obsessive-compulsive disorder is really a terrible thing, bought a lot of useless things back, including but not limited to 3.5-inch screen, Raspberry Pi pin expansion board, 170-hole bread board (too small), rocker module, 4*4 membrane keyboard, temperature sensor, all-in-one relay, Fan. Before the beginning of the heart is very big, want to do some great things, to the last time not enough to react or from the simple to do it    3. Male to male, mother to mother, male to mother of DuPont line each 40. Don't use so much, QWQ, uncomfortable    4.c language code and tutorials is really hard to find, finally found an introduction to WIRINGPI blog, immediately unsanitary environment, now on the market, the Raspberry Pi teaching material is basically based on Python, Python is a very useful language, But the teacher asked to use C language I have no way ah    5.github is useful, but you will use. Learning the proper and graceful porting code is a required course for a program ape.

Author Contact: [email protected] There are no questions in the blog to welcome the letter.

Raspberry Pi Ultrasonic ranging + buzzer (C language)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.