Overview
In this project, we will make a Traffic Light Controller with Raspberry Pi Pico & MicroPython. This project is a simple three-way version of the traffic light controller using Pi Pico and very few components. It has a circuit diagram and the code for the traffic controller system. You can also make 4 Way Traffic Light Controller using ESP32.
Initially, the red light turns on. The red light goes off after the TM1637 Display counts down for 30 seconds. Then the yellow light flashes 5 times with a time interval of 0.3 seconds. Finally, the green light turns on for 10 seconds and then turns off, reciprocating, simulating the effect of traffic lights.
Components Required
In this guide, I used Elecrow Raspberry Pi Pico Starter Kit to test different Modules. You can buy the kit and perform some other operations as well. From this kit, you can use the following components.
1. Raspberry Pi Pico Board -1
2. TM1637 Module – 1
3. Traffic Light Controller Module – 1
4. Breadboard -1
5. Jumper Wires – 4
6. Micro-USB Cable
Need & Logic Behind Traffic Light System
Nowadays there is a huge increase in traffic on roadways. Roads without any supervision or guidance may lead to traffic conflicts and accidents. Therefore Traffic signals are required in order to flow a particular sequence of traffic along with some guidelines. A traffic signal is used as an instructing device that indicates the road user to act as per the displayed sign.
There are different colors in traffic lights. Each light has a meaning, and these lights tell drivers what to do.
- Red light ON – Stop.
- Yellow light ON – Slow down and be ready to stop.
- Greenlight ON – Drive
Circuit Diagram & Connection
The circuit for Traffic Light Controller designed using Raspberry Pi Pico & TM1637 Display is given below.
The Traffic Light Module is a mini-traffic light display with high brightness, very suitable for the production of a traffic light system model.
It has 4 pins. The R, G, B & GND Pins of the Traffic Light Controller Module are connected to GP0, GP1, GP2 & GND of Raspberry Pi Pico.
Similarly, TM1637 has 4 Pins which are VCC, GND, CLK & DIO. The VCC & GND Pin is connected to 5V & GND of Raspberry Pi Pico. The CLK & DIO Pin is connected to GP4 & GP5 of Raspberry Pi Pico.
MicroPython Code/Program
The code for this project is divided into two parts tm1637.py & main.py. This is because the TM1637 Display requires a library. You can refer to the TM1637 & Raspberry Pi Pico interfacing tutorial for more details.
tm1637.py
Open your Thonny IDE and paste the following code in Thonny Editor. Save the file to Raspberry Pi Pico with the name tm1637.py.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
from micropython import const from machine import Pin from time import sleep_us, sleep_ms TM1637_CMD1 = const(64) # 0x40 data command TM1637_CMD2 = const(192) # 0xC0 address command TM1637_CMD3 = const(128) # 0x80 display control command TM1637_DSP_ON = const(8) # 0x08 display on TM1637_DELAY = const(10) # 10us delay between clk/dio pulses TM1637_MSB = const(128) # msb is the decimal point or the colon depending on your display # 0-9, a-z, blank, dash, star _SEGMENTS = bytearray(b'\x3F\x06\x5B\x4F\x66\x6D\x7D\x07\x7F\x6F\x77\x7C\x39\x5E\x79\x71\x3D\x76\x06\x1E\x76\x38\x55\x54\x3F\x73\x67\x50\x6D\x78\x3E\x1C\x2A\x76\x6E\x5B\x00\x40\x63') class TM1637(object): """Library for quad 7-segment LED modules based on the TM1637 LED driver.""" def __init__(self, clk, dio, brightness=7): self.clk = clk self.dio = dio if not 0 <= brightness <= 7: raise ValueError("Brightness out of range") self._brightness = brightness self.clk.init(Pin.OUT, value=0) self.dio.init(Pin.OUT, value=0) sleep_us(TM1637_DELAY) self._write_data_cmd() self._write_dsp_ctrl() def _start(self): self.dio(0) sleep_us(TM1637_DELAY) self.clk(0) sleep_us(TM1637_DELAY) def _stop(self): self.dio(0) sleep_us(TM1637_DELAY) self.clk(1) sleep_us(TM1637_DELAY) self.dio(1) def _write_data_cmd(self): # automatic address increment, normal mode self._start() self._write_byte(TM1637_CMD1) self._stop() def _write_dsp_ctrl(self): # display on, set brightness self._start() self._write_byte(TM1637_CMD3 | TM1637_DSP_ON | self._brightness) self._stop() def _write_byte(self, b): for i in range(8): self.dio((b >> i) & 1) sleep_us(TM1637_DELAY) self.clk(1) sleep_us(TM1637_DELAY) self.clk(0) sleep_us(TM1637_DELAY) self.clk(0) sleep_us(TM1637_DELAY) self.clk(1) sleep_us(TM1637_DELAY) self.clk(0) sleep_us(TM1637_DELAY) def brightness(self, val=None): """Set the display brightness 0-7.""" # brightness 0 = 1/16th pulse width # brightness 7 = 14/16th pulse width if val is None: return self._brightness if not 0 <= val <= 7: raise ValueError("Brightness out of range") self._brightness = val self._write_data_cmd() self._write_dsp_ctrl() def write(self, segments, pos=0): """Display up to 6 segments moving right from a given position. The MSB in the 2nd segment controls the colon between the 2nd and 3rd segments.""" if not 0 <= pos <= 5: raise ValueError("Position out of range") self._write_data_cmd() self._start() self._write_byte(TM1637_CMD2 | pos) for seg in segments: self._write_byte(seg) self._stop() self._write_dsp_ctrl() def encode_digit(self, digit): """Convert a character 0-9, a-f to a segment.""" return _SEGMENTS[digit & 0x0f] def encode_string(self, string): """Convert an up to 4 character length string containing 0-9, a-z, space, dash, star to an array of segments, matching the length of the source string.""" segments = bytearray(len(string)) for i in range(len(string)): segments[i] = self.encode_char(string[i]) return segments def encode_char(self, char): """Convert a character 0-9, a-z, space, dash or star to a segment.""" o = ord(char) if o == 32: return _SEGMENTS[36] # space if o == 42: return _SEGMENTS[38] # star/degrees if o == 45: return _SEGMENTS[37] # dash if o >= 65 and o <= 90: return _SEGMENTS[o-55] # uppercase A-Z if o >= 97 and o <= 122: return _SEGMENTS[o-87] # lowercase a-z if o >= 48 and o <= 57: return _SEGMENTS[o-48] # 0-9 raise ValueError("Character out of range: {:d} '{:s}'".format(o, chr(o))) def hex(self, val): """Display a hex value 0x0000 through 0xffff, right aligned.""" string = '{:04x}'.format(val & 0xffff) self.write(self.encode_string(string)) def number(self, num): """Display a numeric value -999 through 9999, right aligned.""" # limit to range -999 to 9999 num = max(-999, min(num, 9999)) string = '{0: >4d}'.format(num) self.write(self.encode_string(string)) def numbers(self, num1, num2, colon=True): """Display two numeric values -9 through 99, with leading zeros and separated by a colon.""" num1 = max(-9, min(num1, 99)) num2 = max(-9, min(num2, 99)) segments = self.encode_string('{0:0>2d}{1:0>2d}'.format(num1, num2)) if colon: segments[1] |= 0x80 # colon on self.write(segments) def temperature(self, num): if num < -9: self.show('lo') # low elif num > 99: self.show('hi') # high else: string = '{0: >2d}'.format(num) self.write(self.encode_string(string)) self.write([_SEGMENTS[38], _SEGMENTS[12]], 2) # degrees C def show(self, string, colon=False): segments = self.encode_string(string) if len(segments) > 1 and colon: segments[1] |= 128 self.write(segments[:4]) def scroll(self, string, delay=250): segments = string if isinstance(string, list) else self.encode_string(string) data = [0] * 8 data[4:0] = list(segments) for i in range(len(segments) + 5): self.write(data[0+i:4+i]) sleep_ms(delay) class TM1637Decimal(TM1637): """Library for quad 7-segment LED modules based on the TM1637 LED driver. This class is meant to be used with decimal display modules (modules that have a decimal point after each 7-segment LED). """ def encode_string(self, string): """Convert a string to LED segments. Convert an up to 4 character length string containing 0-9, a-z, space, dash, star and '.' to an array of segments, matching the length of the source string.""" segments = bytearray(len(string.replace('.',''))) j = 0 for i in range(len(string)): if string[i] == '.' and j > 0: segments[j-1] |= TM1637_MSB continue segments[j] = self.encode_char(string[i]) j += 1 return segments |
main.py
Open another tab and save this file on Raspberry Pi Pico with name main.py.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
from machine import Pin from time import sleep import tm1637 tm = tm1637.TM1637(clk=Pin(4), dio=Pin(5)) Led_R = Pin(0, Pin.OUT) Led_Y = Pin(1, Pin.OUT) Led_G = Pin(2, Pin.OUT) if __name__ == '__main__': while True: num = 30 Led_R.value(1) for i in range(30): num=num-1 tm.number(num) sleep(1) Led_R.value(0) for i in range(5): Led_Y.value(1) sleep(0.3) Led_Y.value(0) sleep(0.3) Led_G.value(1) sleep(10) Led_G.value(0) |
Working of Traffic Light Controller with Raspberry Pi Pico
Run the script by clicking on play button.
The Red LED will turn on immediately. On the TM1637 Display, a timer will start and will run for 30 seconds till it becomes 0.
Then the yellow light flashes 5 times with a time interval of 0.3 seconds.
Finally the green light turns ON for 10 seconds and then turns OFF.














