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 a 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: About Micro:bit
So let us get started.
Tools You Need- Micro:Bit (1)
- AA batteries (2)
- Alligator clips (4)
- Jumper wires (4)
- Resistor of 220 ohm or 470 ohm (3)
- Breadboard (1)
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. We only need to supply 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. The picture below shows the connection for only one LED.
For this, what I have done is connected PIN0 to the left-hand side of the resistor, so we need to connect the right side of the resistor to the +ve of the LED and -ve of the LED connects to the GND which 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 be using micro python.
MakecodeStep 1
Go to makecode or download Windows 10 makecode app for micro:bit, if you're using 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. 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 secs and turning it off for 1 sec, and the same goes for PIN1 and PIN2 respectively. Now let us see the microPython code.
microPython Code1. Open your mu code editor and create a new project.
2. Import the micro:bit libraries.
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:
while True:
pin0.write_digital(1) # turn pin0 (and the LED) on
sleep(4000) # delay for half a second (4000 milliseconds)
pin0.write_digital(0) # turn pin0 (and the LED) off
sleep(1000)
So the above code will turn on PIN0 for 4 seconds and again turn it off for 1 sec like this.
Full code
from microbit import *
while True:
pin0.write_digital(1) # turn pin0 (and the LED) on
sleep(4000) # delay for half a second (4000 milliseconds)
pin0.write_digital(0) # turn pin0 (and the LED) off
sleep(1000)
pin1.write_digital(1) # turn pin1 (and the LED) on
sleep(4000) # delay for half a second (4000 milliseconds)
pin1.write_digital(0) # turn pin1 (and the LED) off
sleep(1000)
pin2.write_digital(1) # turn pin2 (and the LED) on
sleep(4000) # delay for half a second (4000 milliseconds)
pin2.write_digital(0) # turn pin2 (and the LED) off
sleep(1000)
Javascript codebasic.forever(() => {
pins.digitalWritePin(DigitalPin.P0, 1)
basic.pause(4000)
pins.digitalWritePin(DigitalPin.P0, 0)
basic.pause(1000)
pins.digitalWritePin(DigitalPin.P1, 1)
basic.pause(4000)
pins.digitalWritePin(DigitalPin.P1, 0)
basic.pause(1000)
pins.digitalWritePin(DigitalPin.P2, 1)
basic.pause(4000)
pins.digitalWritePin(DigitalPin.P2, 0)
basic.pause(1000)
})
Demo
Comments