Skip to main content

Interfacing Ultrasonic sensor with raspberry pi

 


Hey,

In today's blog we are Interfacing Ultrasonic sensor with raspberry pi.


WHAT YOU NEED:

Raspberry pi 3

Ultrasonic sensor HC- SR04

680 ohm resistor (1/4 watt)

1.5 k ohm resistor (1/4 watt)

Connecting wire

Mini breadboard

Power supply


CIRCUIT DESIGN :


CODE :

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
TRIG = 16
ECHO = 18
i=0
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIG, False)
print "Calibrating....."
time.sleep(2)
print "Place the object......"
try:
while True:
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False
while GPIO.input(ECHO)==0:
pulse_start = time.time()
while GPIO.input(ECHO)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance+1.15, 2)
if distance<=20 and distance>=5:
print "distance:",distance,"cm"
i=1
if distance>20 and i==1:
print "place the object...."
i=0
time.sleep(2)
except KeyboardInterrupt:
GPIO.cleanup()
Making electronics project is too pretty simple with Us at. Join and Make with Us.

Comments

Popular posts from this blog

How To Make Pac- Man Game Using Arduino

Hello Guys , In This Post We Will Tell You How To Make A Arduino Based Pac- Man Game. For More Awesome Arduino Projects Subscribe Our Blog. #Microelectronics Components Required- 1-Arduino Uno / Nano  2-Display Module  3- Joystick Module  4- Jumper Wire   Circuit Diagram Is Given Below- Source Code- //Nokia 5110 LCD PacMan Game #include <LCD5110_Graph.h> #include <avr/pgmspace.h> #define RST 12    // RESET #define CE  13    // CS #define DC  11    // Data/Command #define DIN  10   // MOSI #define CLK  9    // SCK LCD5110 myGLCD(CLK, DIN, DC, RST, CE); // LCD5110(SCK, MOSI, DC, RST, CS); extern uint8_t SmallFont[]; const uint8_t pacman1[] PROGMEM={ 0x80, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7E, 0x3E, 0x1C,   // 0x0010 (16) pixels 0x0C, 0x00, 0x00, 0x00, 0x1F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF9,   // 0x0020 (32) pixels 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x

Controlling A Servo Motor Using MPU 6050 Sensor .

Hello guys, In this post we are going to tell you that how you can control servo motor using MPU Sensor. Please subscribe to our blog, so that you can't miss any update from us.Join us @Microelectronics. Code and circuit diagram is given post. Material required- 1-MPU Sensor 2-An Arduino 3-Servo motor  4-Some jumper wires Circuit diagram- Source code- // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files // for both classes must be in the include path of your project #include "I2Cdev.h" #include "MPU6050_6Axis_MotionApps20.h" //#include "MPU6050.h" // not necessary if using MotionApps include file // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation // is used in I2Cdev.h #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE     #include "Wire.h" #endif #include <Servo.h> Servo myservo;  // create servo object to control a servo   // class default I2C address is 0x

How To Make A Snake Game Using Arduino

Hello Guys , In This Post We Will Tell You That How To Make A Snake Game Using Arduino.Follow Our Blog To See Awesome Stuffs From Us. Components Required- 1-Arduino Uno 2-4*4 Led Matrix 3-Joystick Module 4-10k Potentiometer Circuit Diagram Is Given Below- Source Code #include "LedControl.h" // LedControl library is used for controlling a LED matrix. Find it using Library Manager or download zip here: https://github.com/wayoda/LedControl // --------------------------------------------------------------- // // ------------------------- user config ------------------------- // // --------------------------------------------------------------- // // there are defined all the pins struct Pin {  static const short joystickX = A2;   // joystick X axis pin  static const short joystickY = A3;   // joystick Y axis pin  static const short joystickVCC = 15; // virtual VCC for the joystick (Analog 1) (to make the joystick connectable right next to the a