arduino

Spark Core

I’ve been spending some time playing with the Spark Core. This device is an open source ARM-based microcontroller with WiFi on-board. It belongs to the Spark OS ecosystem, which aims to be an easy, secure, and scalable solution for connecting devices to graphical interfaces, web services, and other devices. One interesting feature is how you interact with the Spark Core: it has support for mobile devices (iOS or Android), a Web Integrated Development Environment (IDE), and a command line.

The Spark Core devices (also known as “cores”) function in tandem  with the Spark Cloud service (also called the “cloud”) on the internet. The cloud is responsible for managing your cores, developing the core code, and loading applications on your core. Spark Cloud accounts are free and can be created on the Spark build page. Many cores can communicate with each other through a publish/subscribe messaging system made available through the cloud.

IMG_1340

The Spark Core comes in a great package. The box promises that “when the internet spills over into the real world, exciting things happen.” Conveniently, the core comes with a breadboard and a micro USB cable right in the box. This all-inclusiveness makes it ideal for beginners. And it even comes with a sticker!

The easiest way to get your core up and running is to use your mobile device. Simply download the Spark mobile application and connect your mobile device to the same network that the core will use. Turn on your core and make sure it is in listening mode.  Next, use your mobile application to log into your cloud account. You will then be prompted for the network credentials to be used by the core. This will begin a search and registration process where the mobile device finds the core, connects it to the network, and registers the core to your cloud account. The RGB LED on the core shows the status of the internet connection. Once your core is online and registered to your account, you are ready to start playing it!

IMG_1341
IMG_1344

First, I wanted to try interacting with my core from my mobile device. This can be done using a part of the Spark mobile application called Tinker.

IMG_1346

Tinker is more of a prototyping app than it is a dedicated programming environment. It allows you to simulate analog and digital inputs and outputs on the core. Tinker can be integrated with code written for the core so that an application running on your core can interface with the Tinker application on your mobile device.  My experience with Tinker was only so-so as it crashed a number of times on my iPhone 6.

Next, I wanted to try programming my core from the web through the Spark Cloud build website. To do this, I simply logged on to my cloud account which automatically loaded the web IDE. I was curious about how easy it was to import and implement external libraries. To get a feel for this, I tried to connect my core to an LED strip and control it via the Tinker app.

Screen Shot 2014-10-22 at 8.02.45 PM

The web IDE is very clean and easy to use. There are mouse-over tips to help you navigate the environment. The controls (located on the left panel of the IDE) are as follows from top to bottom: flash, verify, save, code, libraries, docs, cores and settings. Double clicking any one of these icons expands and collapses the grey information pane.

The Spark Core language is Arduino compatible as it supports the functions defined in the Arduino language specification. It also includes some extra features that enable you to do things like interact with the network settings and subscribe to specific events from the cloud. Unfortunately, many of the Arduino libraries included in the Arduino IDE have not been implemented for the Spark platform. This may create some problems if you are trying to port your old Arduino code to a core.

Screen Shot 2014-11-05 at 8.58.16 PM

Including the Adafruit NeoPixel library was very easy. I simply searched the available libraries and clicked the import button for the library I wanted to use. All of the necessary includes were automatically inserted into my code. The library display pane also allowed me to browse and/or import the sample code from the library I selected.

Once my code was complete and verified, I simply clicked the flash button and waited for the cloud to update my core. Success!

IMG_1362

Finally, I tried connecting to my core with the Spark Command Line Interface (also called spark-cli). This package is an open source command line tool which uses node.js to program your core. It  works over both WiFi and USB (which is handy when the network is unavailable). The spark-cli tool is not packaged well and was a little tricky to install. After installing node.js, I kept getting compile failures. After some digging I finally got it to work by opening XCode and accepting some license agreements.

The spark-cli tool allows you to interact with your core in a more advanced way. The command line allows you log into the core and read any serial output being generated by the application. It also enables you to manage the application running on a core, such as compiling and uploading new applications or reverting the core to its factory state. Much like Tinker, the spark-cli allows you to simulate both analog and digital input or output. It also enables you to publish and subscribe to events in the cloud so that you can communicate with other cores.

On the hardware front, it is important to note that the internal WiFi chip uses an older version of the 802.11 standard. As the Spark Core uses 802.11b/g, it won’t connect with newer 802.11n networks. I ran into this issue when moving my core between networks. In this case, I had to connect to the core via USB and use a serial connection to enter my network credentials manually. I later discovered that this could also be done via the spark-cli tool.

spark_core_serial

Storing all of your code in the Spark Cloud is both a blessing and a curse. Currently, there is no easy way to version your code or to determine what version of a library is available in the web IDE. I fumbled a bit programming the LED strip because I had to dig around to see which version of the NeoPixel library was available. Additionally, Having the code in a private remote location also makes it harder to share code with other people. Because the core is programmed over the internet, it takes longer to program. This can be too time consuming if you are doing rapid iterative development.  On the positive side, remote code storage and programming means that you can easily modify and upload your application to any core from any web browser. This means no more frantic searching for the correct cable, code version, library version and so on.

To give you an idea how the Spark Core stacks up to other ARM-based microcontrollers, I compared it to two other devices in my project box:

Feature
Spark Core 1.0Arduino DueTeensy 3.1
Processor72 MHz ARM Cortex M384 MHz ARM Cortex M372 MHz ARM Cortex M4
Memory (Flash)
128KB512 KB256 KB
Voltage
3.3v3.3v3.3v
Regulated output voltage
3.3v3.3v and 5v3.3v
Cost
$39$50$20
Size
1.47" x 0.8"4" x 2.1"1.4" x 0.7"
Digital pins
185434
Analog pins
81221
5v tolerant input pins
7021
SPIyesyesyes
UART (Tx/Rx)
143
I2C (SDA/SLC)
122
JTAG
yesyesno
WiFi
yes (802.11 b/g)nono
Programming environmentWeb and Mobile IDE (WiFi),
Command line (USB or WiFi)
Arduino IDE (USB)Arduino IDE + Teensyduino (USB)

The online nature of this device makes it a good choice for people new to Arduino programming. Since the core is internet based, setup is easier than with an Arduino as there are no FTDI drivers to install or serial issues to debug. The RGB LED used for network status is a clever way to assist beginners with debugging connectivity issues. The Spark Core shields are a great starting point for many projects. The Shield Shield makes any Arduino shield compatible with the Spark Core layout, which allows you to take advantage of the large number of Arduino shields already out there. The Spark documentation is very clear and it has a helpful community of users in case you have any questions.

Veteran Arduino programmers can enjoy the advanced features of the Spark OS ecosystem. The distributed nature of the Spark OS makes it simple to connect devices together. The publish/subscribe messaging mechanism allows devices to interact with each other in real time. The RESTful API built into the Spark Cloud makes it easy for any web service to interact with any of your devices on the cloud. On the administrative front, the command line tool gives more power to the user. I was especially pleased that I could use the command line to remotely read the serial output while the core was running.

All in all, I think this is a great board for both beginners and advanced Arduino users. Just like any new device, the Spark Core has some growing pains to work through. Despite that, it offers some great features that make it easy to look past some of the shortcomings.  The on-board WiFi is a real game changer in the hobbyist microcontroller market. I look forward to more internet-enabled projects!

Pulse Jacket

led_image_2 01

This was one of my first Arduino projects. After some near misses with bicyclists while running at night, I decided to get some lights so people could see me in the dark. But why stop at boring plain lights? Wouldn’t it be cool if they could respond to my heart rate?

I looked at a number of existing heart rate sensors for Arduino, but most were optical and could not get accurate readings while I was running since they were constantly being jarred. Since I run with a Garmin GPS watch and heart rate monitor, I tried to hack into the information being sent between the heart rate monitor and the Garmin watch.

Reading a bit more about the technology, I learned that Garmin used the ANT protocol for communication between the watch and heart rate band. The good news was that SparkFun made an ANT transceiver breakout board. The bad news was that the board was discontinued and I could only get my hands on one board. I decided to move forward with this board for prototyping knowing that I would need to come up with a different solution when I made the final project.

The first step was to get the Garmin heart rate monitor and an Arduino communicating with each other. The ANT protocol documents are pretty thorough and they make great bedtime reading. Fortunately for those of us who are impatient, this thread on the SparkFun forums has sample code that already implements the protocol for the Garmin heart rate monitor.

Microcontroller with ANT breakout board

Now that I had the pulse rate information, it was time to add lights. I am a huge fan of Adafruit’s LED strips. These strips have weatherproofing so it would be possible to run outside in the rain. I trimmed the strips to the length of my arms and sealed the ends.

Microcontroller with lights

I added seven different light modes which increased in speed with the heart rate: rainbow, raindrop, range pulse, color shot, twinkle, circulatory and Cylon. Most of these modes are self-explanatory. The range pulse mode faded the strips in time with the pulse and also chose the color based on the current pulse rate (blue being low pulse, red being high). Here you can see a quick demo of the seven modes:

I then began building the final version. For this, I chose to use a Teensy 2.0 because of its low price and small size. I also had to revisit the ANT transceiver. Searching around, I found this ANTAP281M5IB  module with an on-board antenna. After some very delicate wiring and soldering, this proved to be a direct replacement for the SparkFun board.

IMG_8863

Once everything was working, it was time to put this into a portable package for running. The main concern was power. After a bit of research, I found these Energizer power packs that I could plug directly into the Teensy. The one amp power pack would power both LED strips for about an hour. After verifying that everything still worked, I placed the assembled project into a small project box.

IMG_8862

The last issue was how to attach the LED strips to my arms. I thought about embedding them into a jacket by sewing them in, but I decided against that as it would be a pain to clean. Finally, I just glued some cable clips to the back of the LED strips and used velcro straps to adjust them for the right fit.

Assembling the arm supports
Assembled Jacket

And after all that we were ready to go! My first real-world test was the Midnight Run in Central Park on New Years Eve 2013.

New Years Eve - 2013
New Years Eve - 2013

And now I can easily be seen in the dark!

Arduino and Motion

Today I took the Making Things Move With Arduino class taught by Ranjit Bhatnagar at NYC Resistor. I was particularly excited for this class as all of my Arduino projects have been rather stationary. This class was a great overview of some different ways to add motion to your Arduino project. It featured the Adafruit motor party pack.

We had five separate gizmos to play with: one DC motor, one stepper motor, two sizes of servos and one solenoid.

Motors
Left to right: small servo, large servo, solenoid, DC motor, stepper motor

We also assembled the Adafruit motor shield which simplifies using multiple motors with an Arduino. There was a lot of soldering to do up front but it was worth it! Once everything was assembled, we got to play with all of the different types of motors. We even got to keep the motors so the fun could go on all night long!

Arduino and Motors

What was my favorite part of the class? THE MOTOR PARTY!!! WOOOOH!!!

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!

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

 

Microcontrollers!

IMG_6193

Looking through some of my electronics stuff, I realized that I ten different kinds of microcontrollers! That’s not bad at all, considering that I only discovered my love of them last summer. There are five flavors of Arduino: Uno, Uno Ethernet, MEGA, MEGA ADK and Due. There are three flavors of Teensy: Teensy 2.0, Teensy++ 2.0 and Teensy 3.0. There are also two wearables: Lilypad Arduino and Adafruit Flora. So many microcontrollers, so little time!

Motion-Sensitive Paper Lantern

Back in February, I made a motion-sensitive lantern for the Lunar New Year. The idea was simple: have a lantern that appeared to be mostly plain but would reveal a design when a person moved closer to it.

Lantern from far away

The easiest form to use was a round paper lantern. I started with a 14″ white paper lantern and some markers. I then attempted to draw some snakes on it, as 2013 is the year of the snake. As I am not an artist, let’s pretend that these squiggles look like snakes.

Lantern up close

The next part was to add some lighting. The lighting needed to change color. I decided to go with LED strips from Adafruit, as I could wrap them in the center of the lantern and have fairly uniform lighting. I had some left over pieces from a previous project and this seemed like the perfect occasion to use them.

Next, the lighting needed to respond to motion. There were a couple of sensors that would have allowed me to detect motion, but I decided on a passive infrared (PIR) sensor. Interestingly, these sensors work by detecting rapid changes in infrared radiation (including those given off by body heat).

PIR Sensor

The code for this was very straightforward. The PIR sensor sends a high signal on its output pin whenever motion is detected. Therefore, it’s as simple as polling the output pin with digitalRead() and transitioning the lights based on changes in the output pin state.

Finally, I had to find a lightweight power source. I found an Energizer power pack, which was the perfect power supply for Teensy. It even came with a mini USB adapter, which meant that I could plug it directly into the Teensy without having to solder anything. Here is the final internal assembly of the lamp!

The final assembly

And here is the lamp, fully assembled and running!

Dance Dance Revolution Keyboard

Over the weekend, I turned a Dance Dance Revolution dance pad into a keyboard input device. The pad will be used as part of a larger project at the NYC Resistor 2013 Interactive Show. Here is how I did it.

The pad itself is one of those metal arcade style Dance Dance Revolution pads. The only output was a 15 pin D-Sub connector, much like a VGA cable.

Dance Pad
The dance pad connector

Fortunately for me, a fellow Resistor already went through the arduous task of figuring out the pinout for this particular pad. A little further research based on the pinout revealed that this particular model was a TX-1000. The full pinout can be found here.

The next task was to wire the connector to a microcontroller and try to read input from the board. I decided to go with a Teensy as it already has a library that supports keyboard output. In order to have the Teensy be recognized as a HID by a computer, you have to change the USB type. This can be done by changing Tools -> USB Type to “Keyboard + Mouse + Joystick”.

Now I needed to write some code to read the input from the dance pad and convert it to keyboard output. First, I had to define the input pins in the setup function of the Teensy code. This was done with pinMode using pullup resistors.

pinMode(leftPin, INPUT_PULLUP);   // Left arrow

The next step was to read the input from the control pad and send the corresponding keyboard character when a pad was pressed. Fortunately, Teensy offers a Bounce library which makes debouncing switches easy. First you declare a Bounce object on your input pin.

Bounce leftButton = Bounce(leftPin, 400);

Next, poll the pin in the loop() method for state changes. Once a state change is detected, send the corresponding keyboard key.

if (leftButton.update()) {
    if (leftButton.fallingEdge()) {
         Keyboard.set_key1(KEY_LEFT);
    }
}

Once everything was wired up and the Teensy code was running, everything seemed to work… except the right arrow. Unfortunately, this meant that it was time to take the pad apart!

Underneath a panel

The wires

After a bit of investigating and a few false starts, I found that the wires were easily accessible under the up arrow. Thankfully, the wires for each button were a different color so it was easy to determine which wires to test. Testing with a multimeter showed that the right signal wire in the cable was not working. After running a new signal wire for the right pad (and soldering everything else I cut), everything worked as expected! I then used a solderable D-Sub connector and enclosure from RadioShack to make a connector for the pad.

The completed connector

I could then hook the dance pad connector to my homemade connector and then a mini USB to USB cable from the Teensy to my computer. There is nothing more satisfying than navigating your computer by dancing!