Wednesday, February 27, 2013

We interrupt this program…

Let’s say you’ve got a rotating shaft or axle, and you’d like to use an Arduino to measure its speed, say RPM.  First you need some type of sensor.  I found these great little Hall effect (magnetic) sensors at SparkFun.  They’re great because they’re small, they’re sensitive, they’re easy to use, and best of all they’re cheap, under a buck!  I love cheap, especially when I can get something useful and high quality.  This will be a recurring theme in this blog!
SparkFun has links to the datasheet and a tutorial, so I’ll just describe the sensor at a high level: it’s a three terminal device with power, ground and an open-collector output.  The output can drive an Arduino digital input directly, by using the built-in pull-up resistor in the Arduino.  The output of this sensor latches high when brought close to a magnetic North pole, and latches low when brought close to a magnetic South pole.  This works out perfectly if you mount two magnets to the rotating shaft, with opposite poles facing outward.   Thanks to the latching output, you won’t have to worry about “debouncing” the signal to get an accurate reading. 
Next you’ll need some magnets for the Hall effect sensor to well, sense.  I found these small, but strong magnets at Radio Shack that work great, up to an inch away.
Here’s a tip for mounting the magnets to the shaft.  Let them come together, with opposite poles attracting, and use a Sharpie to mark the outside faces.  Let the ink dry, then pry them apart and mount them to the shaft with the marked surfaces facing out.  If the shaft is steel, simply stick the magnets to it, opposite each other.  Then wrap some black electrical tape around them to keep them in place.  For a more permanent mount, maybe use some quick epoxy.
Caution, I’m talking about low speed shafts here.  If you’re measuring say a router or a jet engine, you’re on your own!
OK, so you’ve got your magnets mounted to the shaft, with opposite poles facing out.  Use some thermostat wire or phone wire to connect the sensor to the Arduino: 5V, GND, and digital input 3.  Mount the sensor so the rotating magnets pass within about a half inch. 
Finally, it’s time for software.  I can’t decide which I like better about playing Arduino, hardware or software.  I guess that’s why I keep making new projects, so I get to do both!
We could write a sketch that reads the digital input state, and measures the time it spends high or low, by noting the number of milliseconds since reset.  That would give us the time per revolution.  But that code gets messy and busy, and it just seems like there ought to be an easier way. 
Luckily, two of the Arduino pins can generate interrupts, and interrupts are a perfect solution for this problem.  I used pin 3, which is interrupt number 1.  Here’s the sketch:
/*
  MeasFreqByInterrupt.ino
  miketranch@comcast.net
  open source, use at will
*/

const int encoderPin = 3; // digital input pin 3
const int intNum = 1;     // can generate interrupt number 1
const int LEDpin = 13;    // standard LED pin 13
boolean toggle = false;   // to toggle LED on/off
volatile long count = 0;  // total # interrupts

void setup() {
  pinMode(encoderPin, INPUT);     // set digit pin to input mode
  digitalWrite(encoderPin, HIGH); // enable pull up
  pinMode(LEDpin, OUTPUT);        // set LED pin to output mode
  Serial.begin(9600);             // init serial monitor
  Serial.println("\nMeasFreqByInterrupt\n");
}

void loop() {               
  count = 0;
  // enable interrupt handler, on falling edge of pin 3
  attachInterrupt(intNum, tally, FALLING);  
  delay(1000);             // measure for 1 second
  detachInterrupt(intNum); // disable interrupt handler
  Serial.print(count);   
  Serial.println(" Hz");
  toggle = !toggle;
  digitalWrite(LEDpin, toggle);
}

void tally() {
  count++;     // increment total
}

Tuesday, February 26, 2013

Getting Started with Arduino Uno



I've worked in the electronics industry for 35 years or so, but I started out, way back when, as a hobbyist.

As an FAE for Tektronix, I get to write application software on a semi-regular basis, mostly for controlling test equipment, collecting data, that sort of thing.  But hardware was and is my first love in electronics.   As a kid in grade school I loved tinkering with flashlights, motors, audio circuits, kits, etc.  Later in highschool it was  op-amps and TTL logic.  Back in those days Radio Shack had entire walls full of parts, and working there I often took advantage of the modest employee discount and built up a decent collection of components, bread boards, etc.  And through the years, any household product containing electronics that wears out or breaks, is cannibalized for parts before going to the recycling center.

But over the last few years my electronics parts and projects had gathered dust. Seems like I'd been too busy to play around much with electronics.  What I needed was some fresh inspiration.  And it came in the form of an Amazon gift card.  After checking out music and books, I decided to browse electronics on amazon.com and came across the Arduino microcontrollers.  Turns out they're quite capable and inexpensive.  I ordered my first Uno and within days was off and running, and having fun.

Turns out, Arduino's have been out for several years now and they're really popular among hacker-makers, robot builders, home automation and energy monitoring, and a bunch of guys like me, because these things are a blast!  Based on the Atmel microcontrollers, they have a handful of digital and analog IO pins, NV flash RAM for program storage, I2C and SPI.  It's an open standard and copying and sharing are highly encouraged.  There's a whole cottage industry of parts and accessories ranging from turnkey solutions, to kits, to components.  No matter where you are on the spectrum of hardware experience, there are ways to have fun with Arduino.  And they're very easy to program using a free IDE (integrated development environment) with a simplified C language, and a USB cable connection to a PC, MAC or Linux box.  There are tons of free examples and libraries on the web, along with forums and public domain documentation.  In fact, there's so much information out there, that when you google Arduino you'll get about 26 million hits, and the first 50 or so pages are in fact for the Arduino microcontroller.  There's actually so much information, it can be hard to find what you're looking for.

Here's a book I highly recommend, that seems to be the de facto "bible" for Arduino:

Arduino Cookbook, 2nd Edition (O'Reilly)

This book is perfect for learning Arduino, covering both hardware and software.  And it's also a great reference, with a good index and appendices.  This is the kind of book that I highlight as I read.  Nearly every page has key phrases in bright yellow highlight, so I can quickly find what I'm looking for. The author provides lots of links to more info on the web, and the publisher website provides all the programming source code, and updates to the book text, all for free download.

In future posts I'll recount some of my Arduino projects over the last couple months.  I've made a weather station with wind speed, temperature and humidity; RF wireless links; SD card data logging; real time clock for data logging; small motor control using IR emitter and detector; and most recently I started working with JeeNodes, a great Arduino derivative with RF wireless built in; and installed one in a Kill-A-Watt.