July 2013

Arduino and Sensors

I’ve been busy putting the finishing touches on a class I am teaching at NYC Resistor called Arduino and Sensors. The purpose of this class is to teach people how to use common sensors with Arduino so they can build awesome interactive projects. The class features the Adafruit Sensor Pack 900, as this pack contains a nice selection of common sensors. I’ve written some sample code for each of the sensors in the pack. We will discuss both digital signal and analog signal sensors.

Sensors!

Digital signal sensors are the simplest to use. They simply return a 1 or a 0 based on the reading of the sensor (just like a switch, it’s on or off). Therefore, reading the state of one of these sensors is as simple as hooking the output of the sensor to a digital pin on the Arduino (pins 2-13 on the Uno) and calling digitalRead() on that pin. Here is a simple example – an IR sensor:

IMG_6848

Analog signal sensors are more complex. These sensors return a voltage on an analog pin somewhere from 0 volts to the max voltage of the microcontroller (with the Uno, it’s 5 volts). In order to read an analog sensor value, the sensor output needs to be connected to an analog pin on the Arduino (pins A0-A5 on the Uno). In the code, calling analogRead() on the analog pin will give you the sensor reading. The Arduino automatically converts the voltage on the analog pin to an integer between 0 (no power) and 1023 (full power). Generally, the reading can be mapped back to some meaningful value. For example, here is a simple analog sensor – a temperature sensor:

IMG_6832

According to the datasheet, this sensor returns 0 volts at -50 degrees Celsius and 1.75 volts at 125 degrees Celsius. It has a scale of 10 millivolts per degree Celsius. To get the raw voltage reading, we take our reading value, divide it by 1024 (to get the percentage of the full voltage) and then multiply that by 5 (since the microcontroller is supplying 5 volts). To scale the voltage to the range, we can simply multiply the voltage by 100 (according to our scale factor, 1/100 volt is 1 degree Celsius) and then subtract 50 (since zero volts is -50 degrees Celsius).

Of course, many sensors are more complex than just reading a simple pin. We’ll discuss a number of different scenarios and how to handle them. Did you ever wonder what the AREF pin is for? It’s the analog voltage reference pin and we will be discussing how to use it. We’re also going to use potentiometers to tune the sensitivity of some of the sensors.

The class is already almost sold out! If all goes well, I will hopefully teach it again soon!

General Class Amateur Radio

I upgraded my ham radio license to a general class license today! Yay!

IMG_6784

The test was harder than the technician class test as the material was a bit more technical. I used KB6NU’s No-Nonsense General-Class License Study Guide for the material and eHam.net practice exams. This strategy seemed to work well for me and I will probably do the same thing if I decide to upgrade to the extra class license.

Why upgrade to the general class? The general class gives you more privileges across the amateur radio spectrum. In particular, a large chunk of high frequency bands are only available to general class and above. This is important if you want to talk to people across the world.

My path into amateur radio was a strange one. After my grandfather’s death, my uncle mentioned that amateur call signs were inheritable. Since both of my uncles were already licensed and no one else seemed to be interested, I decided to get my license. After passing my technician class test, I applied to the FCC for a vanity call sign, requesting my grandfather’s old call sign. Coincidentally, the FCC assigned my grandfather’s call sign to me on what would have been his 85th birthday!

KA3USO, the first.
KA3USO, the first.

Interestingly enough, the FCC restricts transferal of cancelled call signs for two years except under two conditions. The first case is that a close family member wants to inherit a call sign. The second case is if a club wants to hold a call sign in memoriam of a former member. It makes me wonder how many call signs are transferred this way.

Blinky Box

This was a gift for my two year old nephew. Since he is a fan of lights and buttons, I wanted to make something blinky for him to enjoy. The concept was simple: make a clear box with buttons and lights that would change color and pattern based on the buttons that were pressed.

Blinky Box

First, I had to find a clear acrylic box large enough for some LEDs, switches, buttons and a microcontroller. I found this great polycarbonate box from Hammond Manufacturing that seemed to be the right size. Next, I needed to find some buttons that could take a beating. Fortunately, Adafruit sells some translucent arcade buttons in bright colors. The lighting was a no-brainer as I am a huge fan of Adafruit’s addressable LED strips. I also found a glowy on/off switch for the power. Somewhere along the way, I thought it would be a cool idea to add a rotary knob so that he could select different blinking patterns.

The next step was to assemble the pieces and wire everything together. The polycarbonate box was harder to work with than I had hoped. The polycarbonate would discolor if I used the laser cutter, so I found myself drilling all of the holes with a rotary tool. I then added the buttons, knob and on/off switch.

Assembly

Once all of the bits were together, I had to add a microcontroller to control the button states and light transitions. I decided to go with the Teensy 3 as it was what I had on hand (and I had yet to work with one). Also, Teensy 3 allows all digital pins to be interrupted (as opposed to four on the Teensy 2), which would simplify reading the button state. The other great reason for selecting the Teensy is that it already has an Encoder library, which makes reading the knob state simple.

The code was easy. Interrupts on the arcade buttons would change a variable representing the color. The interrupt for the black button would kick off a rainbow display routine. In the main loop, I polled for changes in the rotary encoder state and transitioned the lights accordingly. When I was finished, there were five main light colors (white, red, yellow, green and blue), one rainbow routine and six possible blink patterns (always on, fade on/off, blink on/off, chasing light, random twinkling lights and alternating lights).

IMG_6082

I saved the hardest part for last: power. I wanted something that my nephew couldn’t disturb, so anything outside of the box was out of the question. Regular batteries would require opening the box to change, so I thought something rechargeable would work better. I decided to go with a Lithium Ion battery. Unfortunately, these are generally around 3.7 volts and the LED strips require 5 volts. This meant that I needed to find a way to recharge the battery from the outside and find a way to step up the voltage. Fortunately, SparkFun sells a power cell board that does both. Yay!

Power Supply

I added a power jack to the box and used an old 5 volt AC adapter to supply the charging power. I then connected the power cell board and the battery using this handy tutorial. Fortunately, the charging of the battery seemed to work! Unfortunately, the power cell board only provides 600 milliamps at 5 volts, which is not enough power to run a full meter of the LED strip. Sadly, I had to cut the strip in half. It was still impressive even with just one loop of lights! To make the battery last even longer, I also implemented some of the suggestions for conserving power with the Teensy.

The best part about this toy is that it is fully programmable. As he gets older, I can program new features or games into it. Perhaps one day, I can even teach him to program it himself! 🙂

Here is a short video of the assembled box

 

Raspbian

While working on another project, I set up my Raspberry Pi (revision B) with Raspbian. Although it was a bit time consuming, it was not difficult at all. Instead of using the default desktop environment (LXDE),  I decided to try out Raspian Mate as it’s a fork of the well-loved GNOME 2 desktop environment.

Raspbian

I was happy to see that GNOME 2 was still alive and well. I used to run Ubuntu on my home computers, but abandoned it when they switched over to GNOME 3 and Unity. The lack of features in GNOME 3, the clunky Unity interface and the poor device support in Ubuntu pushed me over the edge. I sold out to Macintosh and happily resigned myself to never having to look at xorg.conf ever again.

Raspberry Pi

It surprises me how far computers have come already in my lifetime. As a little girl, I remember playing with my grandfather’s Color Computer 2. It seemed remarkable at the time and I never expected computing to progress as rapidly as it has. For some perspective, the CoCo2 cost $240 in 1983 and had 16KB of RAM and an 8 bit processor at 1 MHz. Twenty years later, the Raspberry Pi costs $35 and has 512 MB of RAM and a 32 bit processor at 700 MHz. It will be interesting to see what the next twenty years brings.