How to not drive a motor with an Arduino

If you are like me, when you were an absolute beginner, you tried to wire a motor up to some batteries, saw the motor was spinning, and thought out loud: “It’s working! How hard could it be to turn it on and off with an Arduino?”

Actually a little more than it seemed at first. There are several problems I encountered and slowly fixed, and I’ll try to sum up in this post the kind of iterative reasoning that led to a decent circuit design.

motor1

So, first thing you try is to attach the battery poles to the motor, keeping the wires in place with your fingers. Everything seems to work fine! So what’s next? What’s the most obvious attempt to control it?

motor2

The second step would be to add a switch. Pushing the switch makes it run, release the switch and it stops. Perfect: now you just have to use an Arduino to open and close that switch.
motor3
This might be the most obvious change for a beginner, but also a very very bad idea. All the current is passing through the Arduino which is, in best cases, limited at 40mA, while even a very small motor will probably need way more than that. This might work for very very small motors but overall you risk damaging the pins.

motor4

So a “better” (but still bad) idea is to use a transistor instead! The configuration above is called emitter-follower. After all, transistor are like switches, and you have a lot of 2n2222 lying around, and they are a buck a kilogram, so why not make use of them? And maybe this circuit is kind of working, but it has at least two issues you should be considering now:

  • What is the current required by the motor? Check the datasheet, and remember it probably needs a way higher amount when it is starting from a still position. The 2n2222 has a maximum rating of 500mA which might not be enough even if your motor is rated to consume only, say, 100mA. A cheap option would be to use a Darlington pair (basically, more transistors), but a better choice would be to use a different transistor which can bear a bigger current, like a MOSFET (e.g. a IRF510, still cheap and easily available) or possibly a relay. (As you probably know there are several models of BJTs, MOSFETs and relays and I’m pretty sure someone skilled enough could write a book about proper choice of this part).
  • the other issue is that a part of the current is still sourced through the Arduino, so before burning something for good, a nice idea would be to add a resistor for protection, so that the maximum current flowing out of the Arduino is limited. (This is probably not an issue if using a MOSFET).

motor5

This will add some protection to the Arduino, but really, why risk? We can change the circuit so that none of the current flowing from the Arduino actually goes to the motor.

motor6

This configuration, called common emitter, is getting better, but there’s still something needed to be done. The motor is a typical inductive load, and when the transistor tries to open the circuit, the motor will try to keep going, forcing a current in the transistor (which is bad and will likely damage it) and/or in the Arduino (which will cause random resets and damage the pins). Another improvement is to add a flyback diode, so that the current has somewhere to go when the circuit is open.

motor7

There. It probably isn’t finished here, but it’s getting on the safe side. Then you might want to drive the motor in reverse, vary its speed, but this will probably be discussed in another post.

(Credits: graphics in this page were drawn with Fritzing.)

2€ charger for any kind of lithium-ion battery

Lithium-ion batteries are everywhere and they are awesome also for hobbyist projects. I’ve been tempted more than once to use old smartphone batteries in my projects, but recharging them might be a problem.

Well, this time I tried my hand at recharging small coin-cells like the LiR2032. I’ve been looking around for commercial chargers, but they seem to be kind of unpopular. Turns out you can do one yourself with less than 2€.wpid-wp-1448223544369.jpeg

The TP4056 (datasheet here) is a little IC which serves the purpose perfectly and currently it can be found on dx.com along with an additional protection circuit for 1.57€. However, this version comes pre-packaged to work with bigger batteries (>1000 mAh) and shouldn’t be used with coin-cells. Lets see why.

The difference lies basically in the charger’s maximum charging current. The previously linked TP4056 uses 1A maximum current, but LiR2032 datasheet recommends a maximum of 35mA. This current is regulated by a resistor placed between pin2 and ground on the charger (which on this board is called R1 and has a value of 1.2kohms). So, looking at the datasheet and doing the maths it turns out a resistance of about 35kohm is needed.

wpid-wp-1448223759418.jpeg

Okay. let’s go. I used

  • the TP4056 board,
  • a coin cell battery holder,
  • a 47Kohm resistor
  • a 10cm ribbon
  • some pin headers, wire, PCB and soldering

The assembling is quite straightforward, I just cut the PCB to size, mounted the TP4056 board on male pin headers, and mounted the coin cell holder over the ribbon, which I added for easier removal of the battery.

I didn’t have a 35Kohm SMD resistor available, so I replaced the old one with a “regular” 47Kohm resistor. Charging will be slower, but basically the slower the better. Soldering was doable although not trivial, a good set of pliers and tweezers definitely did help.

wpid-wp-1448223735401.jpeg

I soldered the wiring on the back… double check for correct polarity.

wpid-wp-1448223555468.jpeg

And voila. Charging an almost completely drained out battery took about 3 hours, during which all the assembly stayed at room temperature (based on a very accurate measurement with my fingers). Green light means charging done.

wpid-wp-1448226374328.jpeg

I still have to figure out exactly how to use this thing with bigger batteries, the main issue being find a way to keep the battery still. This is probably not going to be trivial since Li-ion batteries tend to come in every shape and size. Maybe I could use cable ties or rubber bands… I’m taking suggestions!

Bluetooth low-energy temperature beacon using the nRF24L01: cheap and compatible with existing smartphone apps!

The reason I did this project is because I have a slight overheating issue in a server room; I was wondering if there would be some way to check another room’s temperature from my desk. That’s how I came up with a “wireless thermometer” that can send readings to my Android phone, and my colleagues can use it too!

You might have heard about the nRF24L01: it is a cheap (0.80€) radio frequency module that, back in 2013, Dmitry Grinberg was able to use to “fake” a BTLE beacon. Real Bluetooth 4.0 modules are, nowadays, about 5€ each.

I wondered if I could use it to make a BTLE compatible temperature beacon. Turns out there are 3 major BTLE beacon protocols: iBeacon (by Apple), Eddystone (by Google), and AltBeacon (by Radius Networks). And well, none of them can be emulated with the nRF24L01, since it is only able to send a 16-byte payload (among other limitations), and all three of those protocols require bigger PDUs.

BUT!!! Although I didn’t find a name for it, Nordic Semiconductors Bluetooth ICs (namely the nRF8001 and nRF51822) have their own protocol they use to send telemetry data (which means: temperature, battery level and device ID); turns out this protocol is simple enough to be emulated by the nRF24L01 as well, although with some limitations. They also are so nice to distribute a suite of Android and iOS apps to work with them; the most relevant apps are nRF Master Control Panel (useful for debugging BTLE devices) and nRF Temp 2.0 (a temperature logger; I think it was meant to track device overheating, but hey). You can also download the source code from the app page!

wpid-wp-1444076649110.jpeg  wpid-wp-1444076692964.jpeg
However, the temperature in my room is quite boring, as shown from those screenshots of Nordic Semiconductors apps. (nRF Master Control Panel on the left, nRF Temp 2.0 on the right).

The temperature beacon hardware is very simple. Just wire up your microcontroller to a nRF24L01 and a temperature sensor of your choice (I used a DHT11 I had lying around).

wpid-wp-1444076633700.jpegIt’s more about how you wire them up, I guess.

 rf24-pin
(fabacademy.org)

  1.  GND -> GND on the Arduino
  2. VCC -> 3.3v on the Arduino
  3. CE -> PIN 9 (see below)
  4. CSN -> PIN 10 (see below)
  5. SCK -> PIN 13 on the Arduino Uno
  6. MOSI -> PIN 11 on the Arduino Uno
  7. MISO -> PIN 12 on the Arduino Uno
  8. IRQ -> not used

Be especially careful about the power pin: altough the nRF24L01 can work with your 5v Arduino, you must power it at 3.3v!

While SCK/MOSI/MISO are pretty much wired to 13-11-12 pins in order to use SPI, CE and CSN can be moved to a pin of your choice.

About the DHT11, just wire its output pin to A0, and the other two to ground/+5v.

On the software side, I did a couple modifications on floe’s BTLE library to allow mimicking of the nRF8001 and nRF51822; you can find the BTLE library here. With this library, you can now create your temperature beacon. (Notice I used pins 9 and 10 as CE and CSN, as mentioned previously).

Did you know? Most Arduinos have an onboard temperature sensor! The reasoning behind this probably is that a device malfunction would overheat it. Unfortunately this can’t be easily used as an ambient temperature sensor, because the chip temperature is widely influenced by its heat dissipation. That’s why I used a DHT11. (You can read more about this on Arduino Playground here).

Among other limitations, this setup is not able to use more than 3 of the 40 BT 4.0 channels, and broadcast a name longer than 8 characters (remember that 16 byte limit?). I’m pretty sure it could be used to broadcast the battery level as well.

Sadly, it does not seem somebody has thought of writing a standard protocol for broadcasting some other non-temperature parameter (atmosphere pressure, light, noise level, etc). To a certain extent, nRF Master Control Panel can be used to receive arbitrary data from the Arduino (as float or as integer value), but it’s not suitable for logging and combined with the 16 byte limit it’s quite limited (BT also has a 2 bytes overhead for every field you want to send, hence: 8 bytes device name + 4 bytes temperature + 2*2 bytes of overhead = 16 bytes). If I’m wrong, let me know!

I hope you enjoyed this article!

The knock-activated noisy box

This project was intended as a prank; when I was thinking about the uses of a knock sensor I thought it would be funny to use it to make a doorbell. The thing you see is a small box that produces a funny chirping noise when you knock or shake it. It is intended to be attached to a door, or some other moving object. Not really an Arduino project since it uses only basic electronic components, but still it was quite entertaining to make and it’s a chance to have fun with analog electronics!

I used:

  • a 6x4cm PCB (2.36″ x 1.578″) (obtained by cutting a bigger PCB)
  • one CR2032 coin cell battery
  • a ghetto coin cell battery holder… made out of a binder clip and some insulating tape
  • a KY031 knock sensor
  • a 470uF capacitor (the bigger the better, basically)
  • an active 5V buzzer
  • a very small project box I couldn’t find another use for (8x5x2cm, or 3.1x2x0.8″)
  • wire and soldering iron

The circuit itself is very simple. When you bump the knock sensor, the circuit closes; the capacitor is charged (very quickly) and then slowly discharges thru the buzzer. Note that the knock sensor usually has a digital output/pull-up pin which I’m not using: the 10k resistor it has attached would make the capacitor charge too slowly.

knock-activated-doorbell-schema

The assembly is quite straightforward or in other words I forgot to take pictures of the process.

wpid-wp-1443286858330.jpeg

This is the final circuit. As you can see I used a binder clip for lack of a better coin cell holder… I stole the idea from here. Then I soldered the clip to the PCB. I tried to leave some room in front of the battery to make replacement easier. The cables go from the battery to the back.

wpid-wp-1443286862974.jpeg

The wires on the back make the soldering quite easy. Just solder the knock sensor in series to everything else (green wire – going to ground on the other side), and the capacitor and buzzer (bottom left) together in parallel. Double check the capacitor polarity (red wire goes to Vcc on the other side). I used velcro to attach the PCB to the box; very handy if you, like me, never have available screws of the right size.

wpid-wp-1443286865931.jpeg

The box is just of the right size for the half PCB.

wpid-wp-1443286852087.jpeg

They couldn’t fit any better.

wpid-wp-1443286868985.jpeg

As I mentioned, the box is very small, almost matchbox-sized. The idea is to glue it (maybe with velcro, I like that stuff) to a door or another moving object you want to produce a “pew” sound when you knock or move it.

Some things I learned today:

  • Maybe I should buy a proper coin cell holder.
  • If nothing else, I should have mounted the ghetto holder the other way around.
  • Wires are best cut at the exact length you need them.
  • The KY031 is REALLY poorly sensitive, you have to shake it really hard. They should call it the car crash sensor or the ground-reached-after-freefall sensor or the shake-as-hard-as-you-can sensor or something. It has a very very precise impact angle at which it is somewhat sensitive. I should get a more sensitive vibration sensor, I’ve been looking around and afaik you have these alternatives:

Anyway, the final result for this article is this weird bleeping thing! I hope you enjoyed it!

The Arduino Pro Mini, the Baite BTE13-010, and the ultra low power consumption I achieved thanks to new programming tools

Please welcome my latest programming tool, the Arduino Power Cutter:

IMG_20150726_232059

The BTE13-010 is a cheap Chinese clone of the Arduino Pro Mini, manufactured by Baite, which can be found from their Aliexpress store (among other places). The internets and the producer are unfortunately lacking much documentation about it.

Today I was trying to achieve a very low power consumption on the BTE13-010. You might already have had a look to popular pages when it comes to power saving on the Arduino, two popular ones being Power saving techniques for microprocessors on Gammon Forum and Arduino sleep mode basics on EngBlaze.

However, at first I struggled to achieve the promised great savings, possibly because I was using the BTE13-010 instead of rebuilding a circuit from scratch, like many of them suggest?

To make a long story short, after browsing everywhere, I came up with these two subroutines that will cut (no pun intended) through your power consumption:

The power_saving_init() should be the first thing to be called at setup(), it will initialize all your pins to LOW. power_saving_sleep() will put everything to sleep, and wake up every 8 seconds. With these two, with the regular board (no bootloader modifications, with the power regulator on, etc) when sleeping I was able to achieve 3.24mA of power consumption, which is already pretty good considering it was draining about 10.2mA when fully powered on in the first place.

(I also tried to change the board internal clock with avrdude options, but that didn’t seem to be possible on this board. I also tried to clock it down via clock_prescale_set(), but nothing was gained).

But it doesn’t end here! That’s were the Power Cutter comes in. Thanks to Talpa PCB retrace of the BTE13-010 I was able to elaborate an evil power saving plan:

arduino_cutting

[Credits for this picture to Talpa, with a few corrections/additions by me]

With a cutter, I was able to cut (pun intended) thru power consumption by cutting the two traces in the picture (in light blue in the upper right), thus phisically cutting out the power LED and the regulation circuit (I wasn’t using that regulator anyway, both USB power and my batteries bypass it). The LED and the regulator were draining about 0.47mA and 2.44mA respectively. Note to adventurers: always check with a multimeter before and after attempting to cut traces.

Without those two components, my power consumption went down to 4uA.

For best results, use batteries with low self-discharge rates, like coin cell! AA batteries can have discharge rates up to 30 mA, it’s a lot!

Let me know your thoughts in the comments!