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.
ConnectionsOur 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:
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's how it looks in reality. 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.
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:
Thanks :)
Comments
Please log in or sign up to comment.