In today's lab, we will learn how to use the GPIO zero API to control the output to a servo and read input from a distance sensor.
Tips
Call it servo-input.py, or similar.
The program must continuously prompt and wait for a new angle. It must give feedback if a number outside the range is the input, for example. .
Log the detected distance continuously in centimeters.
Make an LED blink faster when an object is closer and slower as it moves away, and vice versa.
If you don't know how to wire up the circuit, refer to the diagram on the right.
Servos are motor-driven systems that can be programmed.
They are usually comprised of three components: a DC motor, gears, and a potentiometer. They turn depending on the signal received. The potentiometer is able to determine the position the shaft is currently turned to.
Unlike a traditional DC motor, which will continue to spin as long as current flows through it, a servo is able to maintain a set position based on the signal it receives. A series of hIgh and low Voltages over a time period can be used to set the angle. The longer the signal stays high each time is called the Pulse Width.
Servos wiring needs 5V, ground and a signal pin (using PWM).
Use a 5V signal on the red wire.
Ground wire is usually black or brown
A third yellow signal wire can be connected to any numbered GPIO pin.
Ultrasonic sensors measure the distance between the target by emitting ultrasonic sound waves. The “Chirp” is a vibration, or sound wave that fans out in a bell-shape outwards. If the wave encounters an object near enough, that wave will rebound, or echo. The Echo sensor detects this same frequency.
The sensor operates at 5V, while the Raspberry Pi needs to read the incoming signal at 3.3V, so we need to divide the signal with resistors.
The resistor is a passive electrical component to create resistance in the flow of electric current. The resistance is measured in ohms. They can come in all shapes and sizes. The amount of resistance each resistor provides is indicated by the colored bands on the surface. See below for the table reading a resistor value.