The snap:bit is an electronic component for the Snap Circuits educational electronic kit. It features a socket for connecting the BBC micro:bit. This allows the Snap Circuits to be programmatically controlled by the micro:bit.
This project demonstrates how to use a standard TV remote control to send an infrared (IR) signal to the BBC micro:bit to turn on and off the DC Motor (M1) of Snap Circuits.
This project builds on top of what we learned in the Connect Infrared Receiver and the Control DC Motor With Transistor projects.
As mentioned in the Connect Infrared Receiver project, decoding the exact command pattern from the IR remote control requires quite some coding. This project will take a simple way of turning the DC motor on when any of the buttons of the remote control is pushed. Pushing again any of the buttons will turn the DC motor off.
Snap Circuits diagramBuild the circuit shown in the diagram above.
CodeYou can build the code yourself in the MakeCode Editor. You will find the "analog read pin" and "digital write pin" blocks under the Advanced > Pins section.
Alternatively, open the ready project here: https://makecode.microbit.org/#pub:_Lp4DTxRX1LW8
Once ready, download the code to your micro:bit. Then disconnect all cables from your micro:bit. Both the USB and the battery pack must be disconnected from the micro:bit.
How it works...When you close the slide switch (S1), the Battery Holder (B1) at the bottom of the diagram powers the snap:bit through the 3V snap, and the micro:bit turns on. In parallel, the batteries power the Infrared Receiver (U24) through its positive (+) terminal.
At this moment no current flows from pin P2 of the micro:bit to the base of the NPN transistor (Q2). Therefore, no current flows between the collector and the emitter of the transistor, so that part of the circuit is open and the DC Motor (M1) is off.
The "on start" block is the first executed by the micro:bit. The only thing it does is setting the "on" variable to 0. This variable stores the state of the DC motor. "0" means that the DC motor is stopped. "1" means that the DC motor is running.
Next, the “forever” loop starts. It repeatedly reads the analog input from pin P1 where the infrared receiver is connected to.
If you push a button on the IR remote control, it will emit an infrared wave signal to the Infrared Receiver (U24). The receiver will detect it and the analog read from pin P1 will return a low value (< 100).
In response to the received IR signal, the micro:bit will change the value of the "on" variable from "0" to "1" or vice versa.
Let's look closer at the "set 'on' to remainder of ('on' + 1 ) ÷ 2" block. It may look complex in the beginning. In fact, it does these simple calculations:
- It increments "on" by 1. If "on" was 0, it becomes "1". If "on" was 1, it becomes "2".
- It divides the above result by 2 and takes the remainder. If we divide 1 by 2, the remainder is 1. If we divide 2 by 2, the remainder is 0.
- It sets the remainder calculated above as the new value of the "on" variable.
In other words, if "on" was 0, it becomes 1. If "on" was 1, it becomes 0.
After calculating the new value of the "on" variable, the micro:bit writes it as a digital signal to pin P2. The first time we push a button on the remote control, the "on" variable will switch from 0 to 1. So a digital 1 signal will be written to pin P2.
This closes the circuit between the P2 and GND pins and a small current starts flowing to the base of the NPN transistor, through the transistor's emitter, the GND pin of the snap:bit, and back to the negative terminal of the Battery Holder (B1) at the bottom of the diagram.
The small amount flowing through the base of the transistor is enough to trigger the current flow from the collector to the emitter. A large amount of current (up to 200 mA) starts flowing from the positive terminal of the Battery Holder (B1) on the left of the diagram, through the DC Motor (M1), the collector and emitter of the transistor, and back to the negative terminal of Battery Holder (B1) on the left of the diagram. The part of the circuit connected to the DC motor is now closed and the DC motor turns on.
After writing to pin P2, the micro:bit pauses the "forever" loop for 1 second. This is necessary because remote controls send the IR signal in pulses that encode a specific pattern for each button. See the Connect Infrared Receiver project for details. By pausing the loop for 1 second, the micro:bit will discard the remainder of the IR pulse. If it does not to that, it may falsely assume that another button was pushed on the remote control, which would stop the DC motor.
If you push a button again on the remote control, a new IR signal is sent to the infrared receiver. The micro:bit detects it in the "forever" loop and switches the value of the "on" variable from 1 to 0. It now writes a digital 0 signal to pin P2. This opens the circuit between the P2 and GND pins and the current stops flowing to the base of the transistor. This stops the current flow from the collector to the emitter of the transistor and the DC motor turns off.
We remind that we use the Red LED (D1) here as a flyback diode to protect the transistor from damage when the DC motor is turned off. You can learn more about this in the Control DC Motor With Transistor project.
Why are 2 battery holders used?You may have noticed that we have 2 Battery Holders (B1) in this project. The one at the bottom powers the micro:bit and the Infrared Receiver (U24). The other on the left powers the DC Motor (M1).
While the DC motor is running it drives a lot of current from the batteries. This does not leave enough current to the infrared receiver and it may not work properly. This means that if we used a single battery holder for everything, we would be able to start the DC motor, but may not be able to stop it.
This is why we have separate power supply just for the DC motor. You will notice that even with this optimization you need get the remote control closer to the infrared receiver to be able to turn the DC motor off.
Comments