Introduction
Connect a 360˚ Servo to a Bit Board and control it with a Pushbutton and code.
We'll explore code to control the movement of a continuous rotation (360 degree) servo motor using a button.
Video Overview
Featured Document
-
-
The 360 Degree Servo Motor has a 3-wire connector on the end that can plug directly into the pins on the back of the Bit Board.
-
Make sure the Orange Wire is closest to the number 13 for the Pin 13 column.
-
Then the Brown Wire should be closest to the micro:bit (in the - row) and the Red Wire will be in the middle (the +5v row).
-
Connect the Button with Maker Tape, with one side to Pin 5 and the other to GND.
-
-
-
If you've never used a micro:bit before you'll want to check out this guide: Bit Board V2 Setup and Use
-
We're going to load the following code for our 360 Servo Button Both Ways program: https://makecode.microbit.org/_RACbXCFTD...
-
We'll get our button all set in the start block by setting it to up so when the button is shorted to GND (ground) or "pulled down" we can read the state.
-
We'll also set our directionVal variable to 1. We'll use this to keep track of which direction the motor is spinning.
-
In our first forever block we'll check if the button is pressed, and if so, toggle the directionVal value. We do this by checking if it's 0, and if it is, we set it to 1.
-
And if the directionVal value is 1 our if else falls to the else where we set it to 0 instead.
-
In our second forever block we are controlling the motor based on the value of directionVal. It will spin one way or spin other way based on directionVal being either 0 or 1.
-
-
-
Once the code is loaded it should start running immediately.
-
You can power the micro:bit via the USB cable you used to load the code, but now that we are using servos we recommend using a battery pack plugged into the Bit Board.
-
The Blue Pins on the Bit Board provide extra power (5 volts instead of 3 volts) by boosting the voltage coming from the battery pack. This helps improve servo performance, especially with multiple servos.
-
The servo will start spinning in one direction...
-
Press and release the button and the servo will start spinning in the other direction.
-
If you press and hold the button it will switch directions every 500 milliseconds, as specified in the first forever loop.
-
-
-
Follow along with our recorded Live Stream!
-
You can watch the full video of us walking through this project, along with explaining and exploring the code: https://youtube.com/live/HcgJlJ8a2KI
-
