Skip to main content
shopping_basket Basket 0
Log in

Arduino Powered Useless Machine

I saw this project on Arvid Mortenson's website while browsing the internet and thought it would be fun to build my own for our Inner Geek competition at RS.

The idea is simple enough. It is a kind of modern day jack-in-a-box. It just sits there tempting the user to switch it “on”. When you do, the lid opens and a mischievous little leaver emerges and switches itself back “off” again. Each time you activate the switch you get a slightly different pattern of behaviour but it just keeps switching itself off, whatever you do.

As I started to think about how to build such a machine, I realised it was not as easy as I had though. To start with, the box does not actually switch itself off, it just appears that way. It needs to respond quickly to being switched on, so after initialisation the Atmega328 microcontroller inside actually goes to sleep. The box needs to remain active for long periods of time but the servos consume power all the time and would quickly drain the batteries so something needed to be done about that.

I ordered all the parts from RS and decided to prototype the design using an Arduino Uno and breadboard to connect the parts. Then I got to work on building the box out of hardboard to house everything. This took some trial and error but eventually the lid and arm servos where in place and I was able to write as small program using the Arduino Servo library to control the servos and calibrate their starting and stop positions. I then added the IRLZ14PBF N-channel MOSFET to isolate power to the servos during the sleep mode. I connected the gate directly to one of the digital outputs on the Arduino which goes HIGH to switch the MOSFET on. The problem was, it did not always go LOW reliably so I added a pull down resistor to sort out that problem.

title

Next step was to write the main code to put the microcontroller to sleep and wake it using the switch to activate an interrupt service routine (ISR). On the Arduino, only pin D2 & D3 can be used for this. The goToSleep() function uses the deepest sleep mode to save power. It also attaches the ISR. The attachment of the ISR routine is protected to guarantee that it takes place successfully:

void goToSleep() {

set_sleep_mode (SLEEP_MODE_PWR_DOWN);

sleep_enable();

noInterrupts (); // Protects attachment of ISR

attachInterrupt (0, wake, LOW);

EIFR = bit (INTF0);

interrupts ();

sleep_cpu ();

}

It took a while to develop reliable code to wake the processor every time. I used an excellent reference by [Nick Gammon http://www.gammon.com.au/power] who really covers all the details of saving power on the AVR microcontroller and using interrupt service routines.

The wake() function does what it says and wakes the processor allowing the MOSFET to turn on and the servos to activate and run their routines before going back to sleep again.

void wake(){

sleep_disable();

detachInterrupt (0);

}

Another thing I found useful was adding a small LED which comes on when the switch is activated. This was very helpful during debugging to show the processor was actually awake, even if it was not responding correctly.

Once all the code and the mechanism was sorted out, it was time to start up DesignSparkPCB to create a schematic so I could build a permanent version of the circuit. From the datasheets, I calculated that using 4 x 1.2V, 2600mAh rechargeable batteries, both the servos and microcontroller could be powered without using a regulator. This enabled me to pare back the components on the Arduino board to the bare bones. So just a processor, crystal and reset button plus the MOSFET and a few capacitors and resistors. I clocked the Atmega at 16MHz but you could probably go down to 8MHz to save even more power. There are a few essential filter capacitors and also a 100uF electrolytic capacitor across the supply to cater for voltage drops when the servos activate. These parts were all soldered onto an Arduino prototyping board so I did not have to have a PCB manufactured.

title

The tested power consumption in sleep mode came out at just over 0.5mA so the box could in theory remain active in sleep mode for several months without the batteries being recharged.

title

So I had a lot of fun creating this project and learnt a lot about servo control, low power techniques, interrupt servicer routines and MOSFETS that can be put to good use in IoT projects which need to remain dormant for long periods of time and then wake up and do something useful.

I also learnt to use DesignSparkPCB to build and document my circuit. It is a really powerful tool and quite easy to use so I would recommend you try it out.

The schematic, BOM and Arduino code are all available for download on Github, so have a go at building one yourselves, not at all useless really!

title

I'm a big Open Source fan and Linux aficionado with a penchant for Italian motorbikes
DesignSpark Electrical Logolinkedin