Traffic Light System Using BBC MicroBit

In this article, I am going to walk you through the step by step procedure of how to control or blink an LED using the BBC micro:bit and make traffic light system using micro:bit. Basically, I will explain how to control the LED using Micro: bit on a solderless breadboard and how to control brightness. If you're a beginner and if you don't know about micro:bit much then please go through the articles I have posted here.

Hi everyone.
 
In this article, I am going to walk you through the step by step procedure of how to control or blink an LED using the BBC micro:bit and make traffic light system using micro:bit. Basically, I will explain how to control the LED using Micro: bit on a solderless breadboard and how to control brightness. If you're a beginner and if you don't know about micro:bit much then please go through the articles I have posted here.
So let us get started.
 
Tools  You Need 
  1. Micro:Bit(1 pcs)
  2. AA Battery( 2pcs)
  3. Aligator Clip (4 pcs)
  4. Jumper wires(4 pcs)
  5. Resistor  of 220 ohm or 470 ohm(3 pcs)
  6. Breadborad (1 pcs)
Connection
 
We need to connect the GND  to any one of the terminals of the breadboard so that we can use it as GND to connect GND of the LEDs.
Our connection looks something like this and we need to supply the only 3V so we need to use the resistors so that we can limit the current to 3v only 470 ohm is best but you can use 220 ohms also. Below picture shows the connection for only one LED.
 
In this what I have done is I have connected PINO to the left-hand side of the resistor and  we need to connect right side of the resistor to the +ve of the LED and -ve of the LED is connected to the GND that is the last row of the breadboard.
 
Similarly, we need to connect two more LEDs, as shown below.
 
 
 
So this is how I have connected all three LEDs to my micro:bit.
 
 
So let us now see the coding part of this project using makecode and later we will see how we can see using micro python. 
 
Step 1

Go to makecode or download windows 10 makecode app for micro:bit app if you're on windows 10.
 
Step  2

Create a new project.
 
Step 3

Go to Basic and choose forever block.
 
Step 4

Click on Advanced and go to PINS and then digital write pin block and place it inside the forever block and provide some delay too.
 
    
 
We know  0 is low and 1  is high. So when we set  PIN to 0 then it will turn off the light and when we set PIN 1 to 1 then it will turn on the LEDs. This is the code for only one LED; similarly we can code by changing the pins of the micro:bit.
 
  
    
 Full code
 
 
 
So in the above code first I am turning on the PIN0 for 4 sec and turning it off for  1 sec and the same goes for PIN1 and PIN2 respectively.
 
Now let us see the microPython code.
 
1. Open you mu code editor and create a new project. 
 
2. Import the micro:bit libraries.
  1. from microbit import *  
The above code will load the libraries we need to code the micro:bit.
 
3. Now we need to create a loop for turning on and off the LEDs. so we will make a while loop like this.
  1. while True:   
  2.     pin0.write_digital(1)  # turn pin0 (and the LED) on  
  3.     sleep(4000)             # delay for half a second (4000 milliseconds)  
  4.     pin0.write_digital(0)  # turn pin0 (and the LED) off  
  5.     sleep(1000)   
 So the above code will turn on the PIN0 for 4 seconds and again turn it off for 1 sec like this.
Full code
  1. from microbit import *  
  2.   
  3. while True:   
  4.     pin0.write_digital(1)  # turn pin0 (and the LED) on  
  5.     sleep(4000)             # delay for half a second (4000 milliseconds)  
  6.     pin0.write_digital(0)  # turn pin0 (and the LED) off  
  7.     sleep(1000)   
  8.      pin1.write_digital(1)  # turn pin1 (and the LED) on  
  9.     sleep(4000)             # delay for half a second (4000 milliseconds)  
  10.     pin1.write_digital(0)  # turn pin1 (and the LED) off  
  11.     sleep(1000)   
  12.      pin2.write_digital(1)  # turn pin2 (and the LED) on  
  13.     sleep(4000)             # delay for half a second (4000 milliseconds)  
  14.     pin2.write_digital(0)  # turn pin2 (and the LED) off  
  15.     sleep(1000)  
Javascript code
  1. basic.forever(() => {  
  2.     pins.digitalWritePin(DigitalPin.P0, 1)  
  3.     basic.pause(4000)  
  4.     pins.digitalWritePin(DigitalPin.P0, 0)  
  5.     basic.pause(1000)  
  6.     pins.digitalWritePin(DigitalPin.P1, 1)  
  7.     basic.pause(4000)  
  8.     pins.digitalWritePin(DigitalPin.P1, 0)  
  9.     basic.pause(1000)  
  10.     pins.digitalWritePin(DigitalPin.P2, 1)  
  11.     basic.pause(4000)  
  12.     pins.digitalWritePin(DigitalPin.P2, 0)  
  13.     basic.pause(1000)  
  14. })  
Demo
Get code and sketch