Step 1. Get a breadboard to make the job super easy!
Take a look at Raspberry Pi GPIO (General Purpose Input/Output) pin mappings.
In order to create a circuit, we need to know the positions of the pins for the current (5 Volts), the ground (GND), and GPIOs. Please see the picture on the left.
Solderless breadboard with dual bus strips (+ and -) on both sides make the job very easy. We just need to remember:
+ line is where the current wire goes.
- line is where the ground wire goes.
Terminal strips are labeled with a - j and 1-60 and they are where we place sensors and LEDs.
Step 2. Try a simplest circuit.
To warm up, create a circuit to turn on a LED light on the breadboard. You will need a LED light, a resistor, two female-male jump wires, and one male-male jump wire.
YOU MUST TURN OFF THE PI BEFORE CONNECTING ANY WIRES TO AVOID SHORT-CIRCUITING AND RUIN THE PI.
If you have the pi on, then safely shut it down in the command window.
> sudo shutdown -h now
Wait until the yellow light stop blinking (red light is on), then disconnect the power.
Connect a female-male jump wire to a 5V pin-out on the pi board and a + bus port; I used an orange color wire.
Connect a female-male jump wire to a Ground pin-out on the pi board a - bus port; I used an black color wire.
Place one wire from a resistor (100 ohm) on a - bus port and the other on a terminal port on the breadboard, which will take excess current passing the LED light to the ground.
LED light has two legs and one is longer than the other. Place its short leg on a terminal port which is on the same row as the resistor. Place its long leg on a terminal port on a different row as the resistor.
Connect a male-male jump wire to a + bus port and to a terminal port which is on the same row as the long leg of the LED light; I used a green color wire.
Power up the pi board and see the LED light on.
Step 3. Motion Detector
Create a python code that receives signals from a passive nfrared (pir) motion sensor and turns on the LED light if motion is detected. We need to slightly modify the simple LED light circuit we just made to control LED light based on the pir input.
YOU MUST TURN OFF THE PI BEFORE CHANGING ANY WIRES
Remove the green wire from the simple LED circuit.
Connect a female-male jump wire to GPIO #17 port and to a terminal port on the same row as the long leg of the LED light in order power the LED light; I used a yellow color wire.
Make a note of the pins from the pir motion sensor for S (sensor input to the pi board), V (voltage current), and G (ground) and add the sensor on the breadboard.
Connect a female-male jump wire to GPIO #4 on the pi and to a same row as S pin of pir sensor on the breadboard; I used a blue color wire.
Power and ground the pir sensor using male-male wires on the breadboard; I used a white wire for the ground and a red wire for the power.
Turn on the pi board and get to the command terminal.
Create a python code (sensor_motion.py) and run.
> sudo chmod +x sensor_motion.py
> sudo python sensor_motion.py
from gpiozero import LED
from gpiozero import MotionSensor
red_led = LED(17)
pir = MotionSensor(4)
red_led.off()
while True:
pir.wait_for_motion()
print("Motion detected")
red_led.on()
pir.wait_for_no_motion()
red_led.off()
print("Motion stopped")