Difference between revisions of "Ultrasonic Sensor (HC-SR04) + Raspberry Pi"
Jump to navigation
Jump to search
Line 1: | Line 1: | ||
==Hardware== | ==Hardware== | ||
* Raspberry pi 3 | * Raspberry pi 3 | ||
− | * Ultrasonic Sensor - HC-SR04 | + | * Ultrasonic Sensor(s) - HC-SR04 |
* A set of resistors for each sensor you are connecting | * A set of resistors for each sensor you are connecting | ||
** 330Ω and 470Ω | ** 330Ω and 470Ω | ||
Line 17: | Line 17: | ||
*:- This is done because GPIO pins only tolerate maximal 3.3V | *:- This is done because GPIO pins only tolerate maximal 3.3V | ||
Use the breadboard as a connection between the sensor and the pi as done in this circuit diagram | Use the breadboard as a connection between the sensor and the pi as done in this circuit diagram | ||
+ | * An additional sensor can be connected to the pi by mirroring this exact set up on the other half of the breadboard | ||
+ | ** Connect the VCC and GND to the same pins (2 and 6) | ||
+ | ** Choose any two GPIO pins for the TRIG and the ECHO connection | ||
+ | *** Just be sure to account for the correct GPIO pins in your code | ||
[[File:Wire setup.png|250px|thumb|none|Connecting resistors and jumper wires between sensors and pi]] | [[File:Wire setup.png|250px|thumb|none|Connecting resistors and jumper wires between sensors and pi]] | ||
Revision as of 18:27, 2 May 2017
Hardware
- Raspberry pi 3
- Ultrasonic Sensor(s) - HC-SR04
- A set of resistors for each sensor you are connecting
- 330Ω and 470Ω
- Jumper wires to connect the sensor(s) to the pi
- Breadboard to connect the sensor(s) to the pi
Wire setup
There are four pins (labeled) on the sensor that need to be connected to the pins on the Raspberry pi
- VCC to Pin 2 (5v - power)
- GND to Pin 6 (ground)
- TRIG to Pin 12 (GPIO18)
- The 330Ω resistor to ECHO
- - On its end, connect it to Pin 18 (GPIO24)
- - Through a 470Ω resistor, connect it also to Pin6 (ground).
- - This is done because GPIO pins only tolerate maximal 3.3V
Use the breadboard as a connection between the sensor and the pi as done in this circuit diagram
- An additional sensor can be connected to the pi by mirroring this exact set up on the other half of the breadboard
- Connect the VCC and GND to the same pins (2 and 6)
- Choose any two GPIO pins for the TRIG and the ECHO connection
- Just be sure to account for the correct GPIO pins in your code
Software
Python
Create a new script
- Link to repository with script to detect the distance of an object in front of the sensor
Save your script as ultrasonic_distance.py
- Go to File and click on Save as
- In the field Save in browse for the C: drive and then select a folder to save in
- For the field File name type in ultrasonic_distance.py
- In the field Save as type select All Files
- Click on Save
Use the terminal to run the script
- Click on the monitor icon on the top of the screen
- Type cd "folder name" to change directory to your pythonpractice folder, and hit Enter
- Type ultrasonic_distance.py and hit Enter to run your program