Raspberry Pi: LED Blinking using Python

Raspberry Pi: LED Blinking using Python

Introduction

LED blinking is the first lesson for most microcontroller/electronics starters. Same as Raspberry Pi, the GPIO pins on a Raspberry Pi are a great way to interface physical devices like buttons and LEDs with the little Linux processor. The first lesson should be easy to start. So, for this tutorial, I will use serial command line, no LCD monitor, no web browser and GUI. For programming, I will use Python.

 

Note: Original Published 21 Aug 2015, Edited by Azmir Che Azmi

 

0

 

Hardware

  1. Raspberry Pi 
  2. MakerDisk microSD Card with RPi OS
  3. UC00C USB to UART Converter
  4. USB Micro B Cable
  5. Raspberry Pi Power Delivery
  6. Breadboard
  7. Jumper Wire
  8. LED
  9. Resistor

 

Software

PuTTY.

 

Assembly

Step 1: Insert the MakerDisk SD card into your Raspberry Pi, as it comes preinstalled with all the necessary Raspberry Pi drivers and software, allowing you to get started immediately without additional setup.


Step 2: Download PuTTY and open it (Note: no installation required). Connect UC00A to PC and it should be detected as COM port. Set your COM number and baudrate to 115200 bps. Click Open.

 

p1

 

Step 3: Connect UC00C’s transmit, receive and ground pins to Raspberry Pi.

1

Step 4: Connect LAN cable to Raspberry Pi (internet is a must) and power it up. Wait until boot complete. Default login is pi and raspberry for password.

 

p2

 

Step 5: It is better to update and upgrade the Raspberry Pi first. Type:

sudo apt-get update sudo apt-get upgrade

 

Step 6: Then install Python and Raspberry Pi RPi.GPIO Python library. FYI, the newest version of Raspbian has the RPi.GPIO library pre-installed. Type:

sudo apt-get install python-dev sudo apt-get install python-rpi.gpio

 

Step 7: Ok now lets proceed with LED blinking. Connect the LED (to gpio-23) and 330Ω resistor as shown below. You can refer to this link for Raspberry Pi GPIO numbers.

 

RPi LED Blinking

 

Step 8: Create new python file, LED_Blinking.py. Type:

sudo nano LED_Blinking.py

 

Then write the following code. When complete press CTRL+X and then Y.

 

 

 

To run the code, type:

sudo python LED_Blinking.py

 

Now, your LED should blinking at 500ms. To terminate the program press CTRL+C.

 

Step 9: How about controlling the LED using a switch? Lets try. Connect switch to gpio-24 as shown below.

 

RPi LED Push Button

 

Step 10: Repeat step 8 to edit the code. Then run the code and test it.

 

p3

 

REFERENCES

Credit to:

  1. Raspberry Pi: Python scripting the GPIO by scottkildall
  2. Raspberry Pi GPIO by Jimbo
  3. Tutorial: Raspberry Pi GPIO Pins and Phyton by Mark Kleback

 

Thank you

Thank you for reading this tutorial and we hope it helps your project development. If you have any technical inquiries, please post at Telegram group.

 


    สิ่งนี้มีประโยชน์ไหม?    [   Yes ] [   No ]

    อุปกรณ์ฮาร์ดแวร์