Geek's Lecture: Teach you to control traffic lights with Raspberry Pi

Source: Internet
Author: User

hardware involved : Raspberry Pi and related kits, LED traffic lights

knowledge : Circuit Test Board, Canakit

When you're ready, we can start. Use Raspberry Pi for LED prototype control.

The first thing to understand is that access to all of the wires, resistors, and the lights that come with the kit need to be handled with care, after all, if you set the operation improperly, it could damage your hardware.

To simplify the contact with the Raspberry Pi and the LED, and to facilitate the writing of the control code, I decided to write a gadget called Pi traffic light, which is the first step in controlling the LEDs with the Raspberry Pi.

With this "traffic light", all you have to do is plug the gadget directly into the Raspberry Pi's Gpio interface. You do not need to worry about connecting to the circuit board of the wire is correct, do not need to bother to find the resistance to match the LED, not to know where the circuit board where the cathode is the negative.

By default, the PI traffic lights are labeled on the Gpio 10th, 9, 11, and GND interfaces, and these interfaces are positioned next to each other on the PI settings. But in later versions of the Raspberry Pi, these interfaces are in the middle of the Gpio, which is a bit difficult (in case of poor eyesight or the fiber is too dark, it is hard to see). I usually put my own PI traffic light on the 13, 19, 26th Gpio interface and GND interface, which makes it much easier to operate.

Once these interfaces are plugged in, you can open the Raspberry Pi and write some code to control the LEDs. In this article, I'll write some examples. For the first example, I used Python to read the CPU usage of the Raspberry Pi, and to display the red, yellow, and green three LED lights through the Rpi.gpio library.

Principle

If the CPU load is below 50%, a green light is displayed, and a yellow lamp is displayed between 50% and 90%, and a red signal is shown above 90%. Also designed the exception handler, just press CTRL + C to exit the program, so that all the lights will not light. the reason and intent of this design is only one, which is to remind you of the CPU usage of raspberry party through this traffic light.

#!/usr/bin/env python#To use with Pi traffic Light ImportRpi.gpio as GPIOImportPsutil GREEN= 26YELLOW= 13RED= 19#Pin Setup:Gpio.setmode (GPIO). BCM)#Broadcom pin-numbering scheme.gpio.setwarnings (False) gpio.setup (GREEN, GPIO.) Out) Gpio.setup (YELLOW, GPIO. Out) Gpio.setup (RED, GPIO. Out)Try:    while(1): cpu_pc= Psutil.cpu_percent (interval=2)      Print 'CPU:%d%%'%(CPU_PC)ifCPU_PC <= 50: Gpio.output (RED, False) Gpio.output (YELLOW, False) Gpio.output (GREEN, True)if< CPU_PC < 90: Gpio.output (GREEN, False) Gpio.output (RED, False) Gpio.output (YELLOW, True)ifCPU_PC >=90: Gpio.output (GREEN, False) Gpio.output (YELLOW, False) Gpio.output (RED, True)exceptKeyboardinterrupt:Print "Good bye"Gpio.output (GREEN, False) Gpio.output (YELLOW, False) Gpio.output (RED, False )

The second example is a lot simpler than the first one and, to some extent, interesting because I used the scratch to control the LEDs. It is a free visual programming language that allows programmers to use it to create interactive games, stories, and animations. It was originally developed by MIT as a programming concept for children, and it has been a great success.

In order to make scratch with the Gpio interface of the Raspberry Pi, the first thing you have to do is to open the program, enter the editing interface, choose to start the Gpio server. Then you can use the broadcast block to identify the Gpio interface you want to use. In the above, these two broadcast blocks point to Config19out and Gpio19on respectively. And the Gpio 19 is pointing to the red LED light.

For more information on how to connect scratch to the Gpio interface, click here to learn more.

Finally, if you're not good at operating these electronic devices, I hope this introduction to PI traffic lights will make you more interested in Raspberry Pi. If someone wants to learn how to write code, I want to use scratch to make things more interesting, like using it to control LED lights.

* Reference source : opensource

Geek's Lecture: Teach you to control traffic lights with Raspberry Pi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.