LED control, commonly known as "L Chika", is said to be an introduction to Raspberry Pi. I did it on a Raspberry Pi 4 using Python.
The Raspberry Pi acts as a battery, so you can turn on the LED without programming.
Wire like this.
You can do it just by changing the wiring in 1 above.
Reconnect the jumper wire that was connected to pin 1 of "GPIO" to ** pin 11 </ font> ** (6th from the top on the left). Leave the other jumper wire in place.
Wire like this.
In Raspberry Pi 4, you can't blink the LED without doing the following wget.
$ wget https://project-downloads.drogon.net/wiringpi-latest.deb
$ sudo dpkg -i wiringpi-latest.deb #Because it comes out automatically" EnterKey "push
If possible next ...
$ gpio readall
If you get the following GPIO "pin information, you're ready to go.
First of all, let's make L flickering by manual control.
$ gpio -g mode 4 out #LED goes out once
$ gpio -g write 4 1 #Lit
$ gpio -g write 4 0 #Off
Then it will blink automatically.
$ sudo nano led1.py #Create new text
Fill in the following in it
led1.py
#L Chika with one LED
import RPi.GPIO as GPIO
import time
PNO = 17 #GPIO port number on the side connected to the resistor
GPIO.setmode(GPIO.BCM)
GPIO.setup(PNO, GPIO.OUT)
for i in range(15):
GPIO.output(PNO, GPIO.HIGH) #Lit
time.sleep(0.4)
GPIO.output(PNO, GPIO.LOW) #Off
time.sleep(0.4)
GPIO.cleanup()
Press "control" + "X", press "y" and press Enter to complete saving.
In "for i in range ():", enter the number of blinks in "range". In "time.sleep ()", you can enter the lighting time and extinguishing time. As an example, in the text above, it flashes 15 times, turns on for 0.4 seconds, and turns off for 0.4 seconds.
It turns on when it is "HIGH" and turns off when it is "LOW".
$ python led1.py
Run with.
$sudo nano filename#When creating or editing new text
$nano file name#When you just look at the text
$cp File name to copy File name to copy#Copy file
$rm file name#Delete file
$python filename#Execute file program etc.
Add another LED to the wiring of 2 above, and blink the LED alternately.
Insert the other side of the jumper wire that is plugged into pin 1.6 into the minus lane of the breadboard. 2. Insert another set of resistors and LEDs in the same way as in 1 above. 3. Connect the male-female cable of the jumper wire to ** pin 13 </ font> ** (7th from the top on the left) of "GPIO". Insert the other side into the breadboard. 4. Insert the male-male cable of each jumper wire in the same row as the LED "-". Insert the other side into the minus lane.
Wire like this.
$ sudo nano led2.py
led2.py
#L Chika with 2 LEDs
import RPi.GPIO as GPIO
import time, sys
GPIO.setmode(GPIO.BCM)
PORT_L = 17
PORT_R = 27
GPIO.setup(PORT_L, GPIO.OUT)
GPIO.setup(PORT_R, GPIO.OUT)
while True:
try:
GPIO.output(PORT_L, GPIO.HIGH) #Lit left
GPIO.output(PORT_R, GPIO.LOW)
time.sleep(0.4)
GPIO.output(PORT_L, GPIO.LOW) #Right off
GPIO.output(PORT_R, GPIO.HIGH)
time.sleep(0.4)
except KeyboardInterrupt:
GPIO.cleanup()
sys.exit()
Save the file ...
$ python led2.py
Run with.
In this text, L-Chika is made without deciding the number of times, so end with "control" + "C".
Make L-Chika that shines in order and repeats.
Add resistors, LEDs, and jumper wires to the wiring in 3 above.
Wire like this.
$ sudo nano led4.py
led4.py
#L Chika with 4 LEDs
import RPi.GPIO as GPIO
import time, sys
GPIO.setmode(GPIO.BCM)
ports = [17,27,22,10];
for i in ports:
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, GPIO.HIGH)
def led_on(no):
for i, port in enumerate(ports):
if no == i:
v = GPIO.HIGH
else:
v = GPIO.LOW
GPIO.output(port, v)
while True:
try:
for i in range(0, 4):
led_on(i)
time.sleep(0.2)
for i in range(3, -1, -1):
led_on(i)
time.sleep(0.2)
except KeyboardInterrupt:
GPIO.cleanup()
sys.exit()
Save the file ...
$ python led4.py
Run with. Please end with "control" + "C" in the same way as 3 above.
In the article below, a program that reproduces the movement of a traffic light is posted as an application. I would be grateful if you could try it. I made a traffic light-like with Raspberry Pi 4 (Python version)
Whale Flight Desk "Let's make a gadget & simple robot with Raspberry Pi electronic work that starts gently" -My Navi Publishing
Recommended Posts