Introduction
In this article, I am going to write about how we can control or drive servo with Micro:Bit. If you want to know the basics of Micro:Bit Please read
Getting Started with Micro: Bit.
So, let us see how can we do this.
Before starting let us see what tools are needed for this.
Tools You Need
- Micro:Bit (1 pcs)
- USB Cable (1 pcs)
- Battery AAA 1.5 v(2 pcs)
- Battery Box (1 pcs)
- Male Jumper Wire( 3 pcs)
- Mini Servo
I am using the USB cable to power the Micro:Bit, so the battery box is not there in above picture.The battery box is needed only when you need to carry the Micro:Bit.
I have covered all of the basics in
this article so I am going to skip the pre-test and some of the basics and directly go to the connection.
so, let us see the connections.
Connections
Our mini servo needs 3V to operate so we need to give 3v from Micro:Bit to the servo and we need to get output from PIN0 and there is one ground that needs to be connected to the ground.This is how the connection looks:
BBC Micro:Bit |
Mini Servo |
3V |
Red Wire |
GND |
Brown Wire |
PIN0 (P0) |
Orange Wire |
Connect according to the above table:
- Connect 3V from micro:bit to Red Wire of servo.
- Connect GND from micro:bit to Brown Wire of servo.
- Connect PIN0 from micro:bit to Orange wire of servo.
Below is the connection diagram. You can see this is very easy to do.
Here is how it looks in real life. :D. I hope there are no problems in the connection now.
Now, let us move to the coding part. We can code it in two ways -- one is very simple, just drag and drop to another using JavaScript.
- Go to makecode.microbit.org and wait for a few seconds and it will get everything ready. You will see something like this.
Now, if you want to display some message like if I want to display "Serve Demo" on startup of mico:bit then go to Basics and choose Show String Block like this and if you don't want it then just delete this block.
Now, let's move to the servo part. If you want to see your servo moving just once then place your code inside the on start block otherwise place your code inside the forever block. I am going to use forever block in this tutorial.
What is forever block?
Forever block is the code or block that our micro:bit will execute. Forever means if it is
on it will execute the code that is placed inside the forever block. So, I want my servo to rotate continuously as I will place the codes inside the forever block. Following are the steps.
- Delete the on start block if not needed; I will delete it.
- Go to Loop and select While loop block and place it inside the forever block.
- Now, go to Advanced Block, go to pins and select servo and write pins block and place it inside the while loop block.
- Now, we need to add a delay so that our servo will rotate and after some time it will rotate in another direction or according to the angle given by the user. Go to the basic block and choose "pause block" and change the default value to 1000. So, what it will do is that it will provide the delay of 1000 microseconds from one angle to another. That is all.
- Now you just click on both the blocks - servo write pin and pause block; right click on it and select duplicate. Place it just after the pause block and change the angle to 90.
Final code
Javascript code
- basic.forever(() => {
- while (true) {
- pins.servoWritePin(AnalogPin.P0, 45)
- basic.pause(1000)
- pins.servoWritePin(AnalogPin.P0, 90)
- basic.pause(1000)
- pins.servoWritePin(AnalogPin.P0, 180)
- basic.pause(1000)
- }
- })
Now, Click on Download and you will get .Hex file.
Now, copy the .hex file into micro:bit drive. and wait for a second until your micro:bit restarts.
That's all -- now your servo should rotate 45,90 and 180 degrees.
You can see the demo: