In this tutorial we will check how to control a DC motor using the micro:bit board and MicroPython.
Introduction
In this tutorial we will check how to control a DC motor using the micro:bit board and MicroPython.
Since we can’t use the micro:bit to directly power the DC motor, we will use a ULN2803A integrated circuit for that. So, the micro:bit will be responsible for controlling the ULN2803A which, in turn, will allow the motor to be powered or not.
The Micro Bit (also referred to as BBC Micro Bit, stylized as micro:bit) is an open source hardware ARM-based embedded system designed by the BBC for use in computer education in the United Kingdom. The micro:bit is a Single Board Computer (SBC) that contains an application processor with a variety of on-chip peripherals.Other peripherals are connected to this chip. An interface processor is connected to the application processor and manages communications via the USB interface, including the drag-and-drop code flashing process.
You can check the datasheet of the ULN2803A here. You can also check this previous tutorial, which explains in more detail how to use this integrated circuit to control a DC motor.
The electric diagram
System 6 Drive-Loc Nut Driver Blades. Mini Ratchet and MicroBits In Metal Storage Box. Special Price $112.21. List Price Regular Price $124.68. Arm Mbed The Arm Mbed SDK provides standardised drivers for MCU peripherals and abstracts most of the low level hardware details of different MCUs, meaning that micro:bit software can be easily run on other hardware. This includes an abstraction for BLE, the Mbed BLE api.
Since the pins of the micro:bit can only source a maximum current of 5 mA [2], we cannot directly connect the device to a DC motor.
Thus, as already mentioned, we will be using a ULN2803A integrated circuit to provide the current for the motor to operate. The micro:bit will only control if the ULN2803A is providing current to the motor or not.
The connection diagram between the devices can be seen in figure 1. Note that I’m using a 5 V DC motor.
In sum, the ULN2803A will act as a switch, which will turn on / off the connection of the motor to GND, depending if the voltage applied to pin ln 1 is either high or low, respectively.
So, if pin 0 of the micro:bit is in a high level, the ULN2803A will connect the DC motor to GND and thus it will run. If pin 0 of the micro:bit is in a low level, then the ULN2803A disconnects the motor from GND and it will be stopped.
The code
The first thing we will do is importing all the functionalities from the microbit module. With this, we will have access to the objects that will allow us to control the state of the pins, as we will see below.
We will write the rest of our code inside an infinite loop, so we can turn on and off the motor in a pattern that will repeat as long as the program is running.
In MicroPython, each pin of the micro:bit is represented by an object called pinX, where X is the number of the pin [1]. As previously hinted, these objects representing the pins can be imported from the microbit module, like we already did.
Note that, in our case, we have the pin 0 of the micro:bit board connected to the ULN2803A, which means that we will interact with the object named pin0.
In order to set the digital level of a pin, we simply need to call the write_digital method on the pin object, passing as input the value 1 to set it to high, or 0 to set it to low.
So, we will start by setting the digital pin to a high level, thus turning the motor on.
After this, we will delay the execution 4 seconds, so the motor stays on a bit. We will do it by calling the sleep method, which receives as input the number of milliseconds to wait.
This function also belongs to the microbit module. Since we imported all the content from this module in the first line of our code, then we can use the sleep function. You can read more about it here.
To finalize, we will now turn the motor off by calling again the write_digital method, passing as input the value 0. This will set the digital level of the pin to low.
We will also delay the execution 4 seconds, in order for the motor to stay off during some time.
Note that since we have written the previous 4 lines of code inside an infinite while loop, the motor will keep turning on and off as long as the program is running. The final code can be seen below.
Testing the code
To test the code, simply run it on your micro:bit board, after all the connections shown in figure 1 are done. I’ll be using uPyCraft, a MicroPython IDE, to run the previous code on my device.
After the script starts running, the motor should start moving and then stopping in 4 seconds intervals, like shown in the video below.
References
[1] https://microbit-micropython.readthedocs.io/en/latest/tutorials/io.html
Drivers Microsot
[2] https://tech.microbit.org/hardware/edgeconnector/
It looks more like a fancy desk accessory than a precision hand tool you can use to fiddle with electronics and other small items. However, driving screws is exactly what the Kinetic Driver does best. Unlike your standard precision screwdriver, though, the darn thing will open up laptops, fix jewelry, and work on all sorts of small fasteners with a whole load of style.
Billed tongue-in-cheek as “the screwdriver you don’t need,” the tool combines a curvy shape and a polished metal finish with a bit holder at the tip, allowing you to pop in any of the two dozen included bits for driving screws of varying types and sizes. Basically, it’s a precision screwdriver with an excessively elegant design, allowing you to perform jewelry and electronic repairs with the fanciest tool at your disposal.
The Kinetic Driver has a unique shape that combines a shank with a bit holder on one end and a ceramic bearing on the other, with a large brass cylinder, a knurled grip, and an hourglass-shaped stem between them. If that sounds like too much for a small screwdriver, well, it is. And that’s the whole point – it’s a precision driver that puts way too much value on aesthetics, making it a unique hand tool for folks who like fancy stuff in their arsenal.
Drivers Microsoft
The brass cylinder is weighted, so the whole thing produces a lot more pressure than a standard precision driver, allowing you to rely on the weight to drive down screws with little effort, while the knurled patterns ensures a satisfying grip that stays in your hand and keeps you in control the whole time. The hourglass-shaped stem is meant to accommodate the fingers that are not being used to turn the driver, so they don’t have to exert any effort gripping.
The Kinetic Driver is made from stainless steel (except the brass part, of course) with a passivation finish, which gives it a protective oxide layer that leaves the tool immune to corrosion, so this thing should stay looking good even when you end up leaving it in a bag for the next two years before digging it up again like your other precision drivers. Do note, the brass will probably patina over time, so it won’t exactly look the same for the foreseeable future (not that it’s a bad thing). It measures 5.5 x 1 inches (length x diameter).
It comes with 24 microbits, so you get a good selection of driving tips for carrying out repairs and fixes, with a mix of Phillips, Torx, slotted, pentalobe, Y-Type, hex, and U-bit tips. That means, you can go to work on a whole lot of stuff using this thing. And yes, they use standard 4mm precision bits, too, so any precision bits you currently have in your stash will fit right in to fill any gaps in the selection. It comes with a case that has designated slots for the driver and all two dozen bits.
A Kickstarter campaign is currently running for the Kinetic Driver. You can reserve a unit for pledges starting at €75.