I tried L-Chika with Raspberry Pi 4 (Python edition)

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.

Things to prepare

1. Make one LED shine

The Raspberry Pi acts as a battery, so you can turn on the LED without programming.

wiring

  1. Connect the male-female cable of the jumper wire to ** pin 1 </ font> ** (first from the top on the left) of "GPIO".
  2. Insert the other side into the breadboard.
  3. Insert one leg of the resistor in the same row as the jumper wire.
  4. Insert the + side of the LED into the same row as the opposite foot of the resistor.
  • For LEDs, ** long legs are "+" **, and short legs are "-". be careful.
  1. Insert another jumper wire male-female cable in the same row as the LED "-".
  2. Connect the other side to ** 6th pin </ font> ** (third from the top on the right) of "GPIO".

Wire like this. IMG_3800.jpg

2. L Chika with one LED

wiring

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. IMG_3797.jpg

program

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".

  • Please note that the pin number and port number are different.
$ python led1.py

Run with.

Command tips
$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.

3. L Chika with 2 LEDs

Add another LED to the wiring of 2 above, and blink the LED alternately.

wiring

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. IMG_3796.jpg

program


$ 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".

4. L Chika with 4 LEDs

Make L-Chika that shines in order and repeats.

wiring

Add resistors, LEDs, and jumper wires to the wiring in 3 above.

  1. Insert two more sets of resistors and LEDs in the same way as in 1 above.
  2. Connect the male-female cable of the jumper wire to ** pin 15 </ font> ** (8th from the top on the left) of "GPIO". Insert the other side into the breadboard. (To the 3rd LED)
  3. Connect the male-female cable of the jumper wire to ** pin 19 </ font> ** (10th from the top on the left) of "GPIO". Insert the other side into the breadboard. (To the 4th LED)
  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. IMG_3794.jpg

program


$ 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.

application

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)

Reference books

Whale Flight Desk "Let's make a gadget & simple robot with Raspberry Pi electronic work that starts gently" -My Navi Publishing

  • In this article, we have improved the Raspberry Pi 4 version by referring to the above books.

Recommended Posts