Category: Programming

Python Meets the Arduino

I gave a talk at PyCon 2012 on using Python to control external devices through an Arduino.

Seems I was just a wee bit nervous giving the talk. Perhaps some more practice next time…


Tags : , , ,

Twisted RFID

It’s been a while since I worked with Twisted in Python and I forgot how easy and fun it is to create a robust network server with it.

I’m working on a prototype rfid card reader for use at work. We need to track various types of people who come into the facility – volunteers, faculty, youth, Create members, etc and giving them an id card that they can just swipe when they walk in the building would be a boon to help know who’s here and if they should be doing whatever it is that they’re doing.

The folks over at SparkFun have a simple USB based rfid reader that looks like it’ll do just what’s needed. Plug it in to a USB port and the card looks like an old fashion serial port. On my Mac, it comes up as /dev/tty.usbserial-A900UCVB.

The code does two things. First it reads the rfid, verifies that it’s a valid number and then drops it into a holding tank. Second is that it serves up the rfid from the holding tank (along with some supporting data) as json data through an HTTP server. And because its Twisted asynchronous network code, it can basically do both at the same time.

The next step will be to munge this into the existing timecard system and then we can start beta testing it. That’s going to be a bit more challenging. For now, the prototype code is available at Have a look and let me know what you think.

Tags : , ,

Greenbacks and an Arduino

One of the additions that I want to make to Exuro is the ability to accept money and then interact in different ways with the person who shelled out some of their hard earned dollars. In order to do that, I first need to figure out how to accept a money and process the amount given via an Arduino.

I’m working with a Pyramid Technologies Apex 5000 bill acceptor and an Arduino Uno. The folks at Pyramid Technologies seem to be completely open to helping people work with their products. I purchased a used Apex 5000 on ebay and their support folks were quite willing to answer configuration questions and help me with the project. That’s pretty cool.

It turns out that the Apex 5000 is pretty simple to interface with an Arduino. There’s a configuration setup whereby the bill acceptor will send N 50 milli-second pulses per $1 value of the bill that was read. The configuration that I’m currently working with sends 1 pulse per dollar. So a $1 will send 1 pulse, a $5 will send 5 pulses and so on.

The output line on the Apex 5000 is an open collector which makes things pretty simple. The output line of the bill acceptor is connected to a pin on the Arduino that’s used to count pulses. On the Arduino Uno, I’m using pin 2 since that is one of the two that can support interrupts. There’s a 2K resistor that connects the bill reader output / Arduino pin 2 connection to the +5 V pin on the Arduino. And the ground for the bill collector and the Arduino are also connected.

With this hardware configuration, I’m able to read the pulses from the bill collector. My first attempt actually had the configuration so that there were 10 pulses per $1. But this resulted in completely bogus readings because the whole sequence of pulses could take up to 20,000 milli-seconds for a $20 bill. To start, that’s way too long to wait before making the determination as to the bill domination. And the code was slipping sideways when trying to count the pulses. So I reset the configuration to 1 pulse per dollar which results in a maximum of 2,000 milli-seconds for the largest bill allowed.

Right now, the code is still a little wonky. There’s still a problem with determining the actual dollar value. I suspect that it has to do with the relationship between the interrupt and the simplistic logic in the loop to determine the value. Some more debugging is needed.

There’s a code repository to share whatever way I manage to make this work. Feel free to poke at it with a sharp stick.

Tags : ,

Arduino vs Servos

The eyes on Exuro are basically 2 axis gimbals with a servo driving each axis. Never having built anything like this before, it was a bit of a learning experience. One of them was more of a re-learning experience in that I knew this but didn’t remember. On a small microcontroller board like an Arduino, there is a limited amount of power that can be drawn by anything connected to it.

In first working with the Arduino, I connected a single servo so that I could work through the initial hardware setup and some basic programming of the Arduino to make the servo arm move. Ground and power lines were connected to ground and +5v on the Arduino. The control line was connected to pin 9. It went pretty much as expected and was it was fun to see changes in software cause actions in the physical world.

Building on the initial success, I added a 2nd servo so I could start to see to the interactions and hardware layout needed to by the gimbals. Again, the ground and power lines were connected to the Arduino ground and +5v. Control was connected to pin 11. Mostly everything went smoothly but every once in a while, the Arduino would reset. It didn’t happen often so I didn’t spend much time tracing the problem. Until I added the 3rd servo. Ground and power lines were again connected to ground and +5v on the Arduino and the control line to pin 12.

With the 3rd connected and all 3 servos trying to move, the Arduino would reset. It would reset almost immediately after the servos started moving. I changed the code to only move 2 of the servos and things went back to working as expected. Every once in a while the Arduino would reset. But adding the 3rd would cause a reset every time. About this time it dawned on me that I might be trying to draw too much power through the Arduino.

So I dug through my collection of scrap wall warts till I found one that put out 5v at 2 amps. Since each servo wants around 0.5 amps, this should work. So I disconnected the power and ground lines for the 3 servos and connected them to the +5v and ground wires of the wall wart. I added a wire connecting the Arduino ground with the wall wart ground and powered up the Arduino. All three servos started moved their control arms back and forth without any more resets of the Arduino. Yippie!

Tags :

Arduino Switches

Part of the Exuro project includes the setup for a momentary contact switch that can be used to trigger the poofer. This way the system can either be run as a donation machine with the bill reader being used to trip the poofer or it can be manually controlled via a momentary contact switch.

My first attempt at connecting a switch was done by just connecting one switch terminal to ground and the other to the Arduino pin 2. The code was pretty straight forward:

int relay_state = LOW;
void toggle_relay()
  relay_state = !relay_state;
void setup() {
    attachInterrupt(0, toggle_relay, CHANGE); 

This had an number of unfortunate problems. The biggest one was that every once in a while the interrupt would be triggered all by itself . Without doing anything, the poofer relay would trigger. That wasn’t good. (The code for the poofer relay isn’t shown here.) It turns out that the problem was the electrical circuit. Since the switch was just connected between ground and pin 2, the wire running to the switch would act as an antenna and cause the hardware to think that the interrupt should be triggered. To resolve this problem, a 1k resistor was placed between pin 2 and ground. This ensured that the electrical signal traveling over the switch wire was stable.

The second annoying problem was that every once in a while an interrupt would be missed and the relay_state would be wrong. The toggle_relay code merely sets the relay_state to the inverse of what it was. That is if it’s a 1 then it becomes a 0 and if it’s a 0 then it becomes a 1. So if an interrupt is missed then the relay_state would be wrong and the poofer relay would be be the inverse of what it was supposed to be.

To resolve that, the toggle_relay code was changed to:

void toggle_relay()
  int val = digitalRead(2);
  if (val == LOW) {
    relay_state = LOW;
  } else {
    relay_state = HIGH;

This way the relay_state is always set to the state of the switch as read off pin 2. With these two changes, adding a 1k resistor and directly reading the pin, the system is much more stable and works as needed. Yea!

Many thanks to CTP for the pointer to using a resister to stabilize the switch.

Tags : ,