Arduino Project 5: Internet Control without Ethernet Shield

February 18, 2013 22 comments

I’m getting ready to start an Arduino home automation project, so I started looking at ways to interface with an Arduino across the internet. That way I’ll be able to control all of the lights, locks, etc. anywhere I have internet access.

Now, the obvious answer was to buy an Ethernet shield, but I already run a home web server so that seemed unnecessary. I saw a few solutions using Processing or Python scripts, but that seemed unnecessarily complicated. It took a fair amount of digging and brainstorming, but I’ve found an ultra-easy, ultra-flexible, and ultra-fast solution. Ultra.

There are a few major assumptions here.

  1. You have a computer running a web server that is accessible from the open internet, using a static IP address or a Dynamic DNS name. Your computer is either directly connected to your ISP and using a public IP address or your router is set up for port forwarding. If you don’t have this set up yet, just Google it – there are a lot of tutorials that explain how to set up a home web server.
  2. Your home web server has PHP. If not, this will still work, you’ll just have to re-write it in Java or whatever server-side language you’re using.
  3. The Arduino will be plugged into the web server via USB.

Here’s the basic concept: the Arduino can’t read files from the server via the USB serial connection, so the server will have to “push” the message. The server side code (PHP, Java, whatever you choose) cannot talk directly to the serial COM port, so we need a local script on the web server that can talk to the serial (USB) port. Last, the Arduino sketch has to be written so it can “catch” and process the message.

In this example, we’ll just create a barebones web page for controls that will turn an LED on and off via the Arduino. Let’s start with the sketch. The sketch is just listening on the serial connection for a 1 or 0. If it receives a 1 it will turn the LED on, if it receives a 0 it will turn the LED off.

Alex Glover
February 2013

void setup() {
//set the LED pin to OUTPUT
pinMode(13, OUTPUT);

void loop() {
//wait until the serial connection is open
while (Serial.available() ==0);

//read from the serial connection; the - '0' is to cast the values as the int and not the ASCII code
int val = - '0';

//print to the console for testing

//if we've recieved a '1', turn on the LED and print a message
Serial.println("Received a 1");
digitalWrite(13, HIGH);
//if we've recieved a '0', turn off the LED and print a message
Serial.println("Received a 0");
digitalWrite(13, LOW);

Pretty straightforward, right? OK, now we need two scripts, one to send a ‘1’ and one to send a ‘0’. In Windows, simply create a text file (call it whatever you want), and give it a .bat extension. In my setup, my files are called serial_out_0.bat and serial_out_1.bat. Each script has only one line of code.

ECHO 0 > COM3:


ECHO 1 > COM3:

Note that you might have to change the COM designation. You can check which COM your Arduino is connected to by looking in the Arduino IDE under Tools –> Serial Port. If you’re not using Windows, you should be able to do this pretty easily in a shell script. At this point you can test to see if the batch scripts will turn the LED off and on. Also ensure that the web server will be able to execute these scripts (don’t assign user-specific privileges or save them in protected directories). If you’re unsure, just put these scripts in the same web root directory where you’ll host your web page.

Easy right? Alright, the last piece is the web form. All you need are buttons within a form that will then execute the batch scripts we wrote earlier. Easiest solution is to use ‘submit’ buttons and then check for which post variables are set. The rest of the code is very straightforward so I’ll let it speak for itself.

if(isset($_POST[‘submitOn’])) {
else if(isset($_POST[‘submitOff’])) {
<form action=”control.php” method=”post”>
<input type=”submit” name=”submitOn” value=”Submit On”>
<input type=”submit” name=”submitOff” value=”Submit Off”>


You’ll have to change the paths to correspond with the location of your scripts, but otherwise that’s it. Add some CSS if you want to add some polish to your controls. This is a very simple example, but you should be able to adapt this code to any project.

Arduino Project 4: Tilt To Unlock

February 7, 2013 10 comments

This is my favorite Arduino project to date. After some basic experimenting with an accelerometer, I had an idea. What if you used the accelerometer to look for a combination of movements? Instead of using a combination lock or a traditional key lock, you could just tilt the object in a set pattern to unlock it. Here’s what I ended up with:

Pretty sweet if you ask me.

Read more…

WordPress: Twenty Eleven Child Theme with Galleria Slider

January 28, 2013 Leave a comment

OK – I’ve been spending the last few hours getting acquainted with the WordPress backend (that sounds more risque than I had intended) and making my first custom ‘child’ theme.

If you have an interest in making custom themes and you’re just getting started, I’ve got a few very generic tips:

  • Don’t underestimate how complex some of the themes and frameworks can be
  • Start by creating a child theme or modifying an existing theme
  • Check out this tutorial on PressCoders

The tutorial on PressCoders is very clear, so I won’t re-invent the wheel and write a tutorial on making child themes. Instead, I wanted to share the child theme that I made. Basically, I’ve taken the Twenty Eleven theme and replaced the static header image with a dynamic image slider (using Galleria). You can check out a live demo, otherwise here’s a quick screenshot:


Anyways, as simple as it is, I’ve learned a lot from the exercise. If you want to use the theme or take a look at the code, here’s the package.

Thanks for reading.

Arduino Project 3: Accelerometer Primer

January 21, 2013 2 comments

This project is more of a warm-up than a full-blown project. I wanted to get a feel for the basic workings of an accelerometer in preparation for a larger undertaking (no spoilers!). I borrowed this project idea from a forum about Arduino and accelerometers but can’t remember which forum, so I can’t credit back to the original poster – sorry about that.

Before we get to the how, let’s take a look at the what. Basically we have an accelerometer and 5 LEDs mounted on a breadboard. Four of the LEDs are mounted to correspond with the X and Y accelerometer axes and one LED is in the middle.   When the accelerometer is level, only the middle LED is illuminated. If you tilt the accelerometer to the side, the LED on that side is illuminated.

Read more…

Bootable USB with UNetbootin

January 14, 2013 Leave a comment

OK, so you have a netbook or ultrabook with no optical drive. Or maybe your optical drive is dead. Or maybe you’re smart enough to not use optical drives in general. Whatever the situation is, you want to run or install something from a bootable USB drive.

There are lots of solutions out there, but nothing beats the Universal Netboot Installer, or UNetbootin. UNetbootin has a simple GUI and never gives any grief. You can use your own ISO that you’ve downloaded or use one of the 42 ISOs UNetbootin provides, including BackTrack, CentOS, Ubuntu, as well as several rescue and recovery utilities.

Using UNetbootin is stupid easy. Grab a USB flash drive with enough storage to hold your ISO. Better yet – pick up a 16GB flash drive for $10 and it will be big enough to hold any ISO you could ever want. Got your USB flash drive? Let’s get started.

  1. Plug your USB flash drive into your computer. Note the drive assignment; in Windows, it will probably be D:\ or E:\, or some other letter. For the Unix users, your drive designation will probably be sda, sdb, sdc… sdx. Remember the drive assignment.
  2. Go to UNetbootin’s page and download the correct version for your OS.
  3. If you have a particular ISO you want to use, download it. Otherwise if you want to use one of the included ISOs, skip this step.
  4. Open UNetbootin. If you want to use one of the ISOs they provide, check the “Distribution” radio button and choose an ISO from the dropdown

    Thanks to UNetbootin for this image.

    If you want to use your own ISO, click the “Diskimage” radio button, click the browse button ” … ” and find your ISO that you downloaded.

  5. Change the drive to correspond with your USB flash drive. If you can’t find your drive, make sure it’s mounted and then restart UNetbootin.
  6. Click OK. If you chose a distribution provided by UNetbootin, it will take a fair amount of time to download the ISO before it can write to your USB drive. This is normal.
  7. Once the USB has been formatted and the ISO written to it, UNetbootin will restart your computer in an attempt to boot from the USB drive. If it doesn’t work, go into your BIOS settings and try changing the boot order.

Donezo. Hope you found this useful.

Arduino Project 2: The Audible Eye Part 2

January 9, 2013 Leave a comment

Alright, if you’re following along from Part 1, you should have your headphones/speakers as well as your ultrasonic rangefinder wired up and operational. Next we have to write the code to marry these two devices.

The Code

Alex Glover
Copyright December 2012
The Audible Eye - handheld device that utilizes an ultrasonic rangefinder to determine distance to whatever object the device is pointed at.
Feedback will be delivered to operator via sound - tone will increase pitch for shorter distances, no tones played for extreme distances.

Credit to:
YourDuino SKETCH UltraSonic Serial 1.0

Uses: Ultrasonic Library (Copy to Arduino Library folder)

/* Libraries */
#include "Ultrasonic.h"
/* Pin Assignments */
//Pin designated for controlling the emission of ultrasonic 'pings'
#define TRIG_PIN 2
//Pin designated to 'listen' for ultrasonic 'pings'
#define ECHO_PIN 7
//Pin used to drive the audio output through the headphones
#define SOUND_PIN 12
/* Declare objects */
//Create an object of type 'Ultrasonic' and call it 'Sensor,' passing the trig pin and echo pin numbers into the constructor
Ultrasonic Sensor(TRIG_PIN, ECHO_PIN);
/*-----( Declare Variables )-----*/
//Variable to hold the tone value; this value will be calculated first, and then passed to the tone() function
int toneToPlay=0;

void setup()
//Pring the following text to the serial monitor
Serial.println("The Audible Eye");

void loop()
delay(200); //20 millisend delay to let echos from room dissipate

//The sensor occasionally returns 0 incorrectly. 0 would also give us a divide by zero error in the next line of code. To address these issues
//we use this simple if statement. Basically, if a 0 is returned, we play no tones through the headphones.
if (Sensor.Ranging(INC) != 0){
//Calculating the tone to play as 1000 hertz divided by the number of inches. So if the device is one inch away from an obstacle, we play
//a 1000 hertz tone. If the device is 10 inches away, it plays a 100 hertz tone. Anything greater than 20 inches reduces the tone to almost inaudible.
//Here we take whatever value toneToPlay was calculated at and pass it into the tone() function. The parameter of 200 is the number of milliseconds to play
//the tone. The loop takes about 200 milliseconds in total, so if the loop hangs or crashes, the tone won't keep getting played indefinitely.
tone(SOUND_PIN,toneToPlay, 200);

Upload that sketch to your Arduino and test. If all’s well, you should be hearing various tones coming out of your headphones depending on what’s in front of your rangefinder.

The Finishing Touches

Now at this point, you could declare this project a success. The device works as intended. However, I wanted to give this project a few finishing touches that I usually skip over, so let’s put it all inside an enclosure, make it portable (i.e. battery powered), and throw on a badass toggle switch for the fun of it.

First, let’s wire up the toggle switch. Take one of your 9V battery adapters and cut both wires about halfway between the battery clip and the male adapter. Now take the battery clip side and strip the wires. Connect the red wire to the voltage-in connection on the switch and the black wire to the ground. Now take the male adapter piece and strip the wires. Connect the red wire to the voltage-out connection on the switch and connect the black wire to the same ground connection on the switch we used before. My wiring is quite ugly, but you should get something that looks like this:


Now for the enclosure. I was having a hard time finding a legitimate project enclosure of the right size, so I just used an old cardboard box. Cut out a small rectangle for the the rangefinder to protrude from at the front of your device. On a different side, cut out another hole (size will depend on what size of toggle switch you use). Once you have your holes cut, start installing your components. For my enclosure, I installed everything but the Arduino on one ‘level’ and used some packing foam to wedge things into place.


Then I installed the Arduino and connected the power adapter, leaving the headphones outside of the box as I closed the lid. Here’s a final closeup:


That’s it, hope you enjoyed the post!

Arduino Project 2: The Audible Eye Part 1

January 7, 2013 6 comments

The Audible Eye is a short proof-of-concept project that I came up with while looking for ways to experiment with some new gear. The idea of combining an ultrasonic rangefinder and some sort of signaling audio output jumped out to me – it would give you a depth perception, not unlike echo location that bats and dolphins use. As far as practical use, I believe it could be used by the visually impaired as a complement to a white cane, but not as a replacement.

Basically, the tone/pitch of the audio signal would get higher as the operator moves closer to a wall or object, indicating to the operator that they are getting closer. Similarly, as the operator moves away from the wall the tone will drop until it’s almost inaudible.

Let me provide some context to make this more clear. Let’s imagine our operator is blindfolded. If the operator pointed the device down an empty hallway for example, they would hear almost no tone, telling them it’s safe to walk forward. As they approached a wall, the tone would increase. The operator would then scan around them with the device, find another path that was unobstructed, and could continue walking.

This video is all but unwatchable (quality is IMPRESSIVELY bad), but it will at least give you an idea of how it works. And of course I disassembled the project before I realized the video was botched.

Alright, let’s open up the enclosure and start breaking down the project. The entire project can be broken down into just a few components. Here’s a view inside of the enclosure.


Component List:

Building the project can be broken down into phases. Let’s focus on the ultrasonic rangefinder first, and build from there.

Setting up the Ultrasonic Rangefinder

There are 4 pins on the HC-SR04 ultrasonic sensor module: voltage in (labeled “Vcc” on my module), ground, TRIG, and ECHO. The TRIG pin will control the transmission of ultrasound. The ECHO pin will be the receiver pin. Use your jumper wires to connect the ground on the module to the ground on the Arduino (red wire near the top of the above picture) and connect the voltage in pin to the +5v pin on the Arduion (white wire near the top of the above picture). Next, use jumper wires to connect the TRIG and ECHO pins to two of the digital pins on the Arduino. These will be the numbered pins on the Arduino that do NOT have the tilde (~) prefix. In my build, I used pin 2 for the TRIG connection and pin 7 for the ECHO connection. Here’s a better picture:


OK we’re all wired up! There are several libraries for interacting with ultrasonic rangefinders. The one I used is probably out of date, but you can download it here.

Note: If you’re having issues with this library, edit Ultrasonic.cpp and Ultrasonic.h. Replace this line:
#include “WProgram.h”

with this:
#include “Arduino.h”

Once you’ve downloaded it, add the entire directory (should contain Ultrasonic.h, Ultrasonic.cpp, keywords.txt, and an examples directory) to your Arduino’s libraries directory. I’m being lazy and have the Arduino install on my desktop, so my library directory is here:

Once that’s done, open up the Arduino IDE. Click on the “Sketch” dropdown menu, click “Import Library” and verify that Ultrasonic is a listed library. If it’s there, you’re in good shape.

Visit and check out the Arduino Sketch provided there. Edit the TRIG_PIN and ECHO_PIN variables to coincide with the pins you used (remember I used 2 and 7 in my case). Connect your Arduino via USB and upload the Sketch. Finally, open the serial monitor (Tools dropdown –> Serial Monitor). You should see readings in inches and centimeters being reported back to the serial monitor.

Awesome – this is our first building block.

Setting up the Audio Output

This step is super easy, unless you’re me and you waste an hour getting stereo wires confused. If you’re using a piezo-electric piece or a simple mono-speaker or headphones, there should be only 2 wires – a voltage in and a ground. Simple. Now, the scrap headphones I had were stereo headphones, which threw me off. If you have a stereo device, you’ll likely have 2 separate insulated sections with 2 types of wiring each. In each insulated wire is one plain copper wire and one painted wire. The copper wires are both grounds and the painted wires are the right and left voltage-in connections. Since we’re just doing simple mono output, go ahead and twist the two copper wires together and twist the two painted wires together. Next, connect the copper ground wires to one of the grounds on the Arduino. For the voltage-in wires, connect them to one of the PWM pins on the Arduino (I used pin 11). Here’s a closeup of the headphone wiring:


Now to test! Conveniently, the Arduino IDE (at least version 1.01) includes a few audio output sketches. Go to File –> Examples –>2. Digial –> Tone Melody. Search the Sketch for the tone() function – change the pin number in each of these functions to coincide with the pin you connected your headphones/speaker to. Now upload the Sketch to your Arduino – if everything is working, you should hear some the melody of “Shave and a haircut, two bits.”

Awesome – now we have two of our building blocks completed.

Cliffhanger! In part 2, we’ll wire up the toggle switch and battery, as well as write the code to combine the rangefinder and the headphones to complete “The Audible Eye.”

%d bloggers like this: