Micro:bit MicroPython: controlling a relay

In this tutorial we will check how to control relay using the micro:bit board and MicroPython. The tutorial was tested using a DFRobot relay board.

Introduction

In this tutorial we will check how to control relay using the micro:bit board and MicroPython.

We are going to focus on how to control the relay using the micro:bit board, but we are not going to connect the relay to any other device. Naturally the relay can then be used to control a wide variety of devices, such as lamps or fans.

Please take in consideration that working with the mains is dangerous if you don’t know what your are doing. Stay safe and don’t work with the mains if you don’t have experience with it.

The tutorial was tested using a DFRobot relay board.

The electrical diagram

As already mentioned in the introductory section, this tutorial was tested using a DFRobot relay board.

One of the greatest advantages is that it already contains all the electronics needed for the relay to be directly controlled from a microcontroller pin without causing any damage to it.

Thus, we simply need to power the relay board and connect a digital pin of the micro:bit to the signal pin of the relay board. The full electrical diagram can be seen below in figure 1.

Electric diagram of the connection between the micro:bit and a relay board.
Figure 1 -Electric diagram for the connection between the micro:bit and the relay board.

As can be seen, we are powering the relay board with 3.3v. Additionally, we need to have a common ground between the micro:bit and the relay board.

Note that, in figure 1, we labeled the input to control the relay state as SIG. This is a generic labeling since depending on the relay board used the name can vary.

For the code below, we will assume that we will be using digital pin 0 of the micro:bit.

The code

We will start the code by importing all the content of the microbit module. By doing this, we will have access to the pin objects that allow us to control the state of the micro:bit digital pins (more precisely, we can control if they are at a high or low voltage level).

from microbit import *

The implementation of the rest of the code will be done inside an infinite loop, so we keep the relay switching between an On and Off state periodically.

Note however that this is done only for demonstration purposes. Relays are mechanical devices that have a limited amount of cycles they can endure before failing. So, in a real application scenario, we should only switch their state when needed.

while True:
    # relay state switching code

As already mentioned, when importing the microbit module, we should have access to some objects representing the pins of the device. So, each pin of the micro:bit is represented by an object called pinX, where X is the number of the pin.

Since we are using pin 0 to control the relay (as indicated in figure 1), then we should interact with an object called pin0.

To set the digital level of a pin, we can call the write_digital method on the pin object. This method receives as input the value 0 or 1, which indicates that we want to set the output level to low or high, respectively.

Since we want to alternate between the states of the relay, we will start by setting the pin to a high level. Then, after a delay of 5 seconds, we will set the pin to a low level.

After this we will do another 5 seconds delay and end the iteration of the loop. This pattern should then repeat as long as the program is running.

pin0.write_digital(1)
sleep(5000)
pin0.write_digital(0)
sleep(5000)

The complete code can be seen below.

from microbit import *
 
while True:
    pin0.write_digital(1)
    sleep(5000)
    pin0.write_digital(0)
    sleep(5000)

Testing the code

Assuming that you have already wired the micro:bit and the relay board accordingly to the electric schematic of figure 1, simply run the script from the previous section on the micro:bit.

In my case I’m using uPyCraft, a MicroPython IDE. Once the program starts running, you should obtain a result similar to what is shown in the video below.

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading