Written by frederick | February 22nd, 2010 | LiFT TV

We’re trying out a few new things with this week’s podcast. For one, we’ve received feedback that some of our podcasts run a bit long, so this time I kept it under 5 minutes. It’s also my first tutorial for LSB. The tutorial includes both a hardware and a code component, so I decided to focus on the hardware in the video and write about the code here.

Hardware Hacking : Interactive Devices

There’s a growing grass-roots movement of people who are taking part in the hitherto hermetically sealed world of interactive device design. No longer are you limited to products available at your local dealer; from circuit bending to microcontrollers the open source hardware movement brings you moreā„¢.

MAKE Magazine exemplifies the spirit of this movement with its open approach to sharing knowledge. Along with providing great information resources, MAKE has an online store where they provide hardware and supplies for people ready to get into hardware hacking. We recently bought a couple Arduino kits from MAKE magazine:
Getting Started with Arduino Kit
Arduino Projects Pack

The Arduino is basically a tiny little computer which can be easily programmed and used to build the interactive device of your dreams. You write programs for it on your computer and then load them to the Arduino via USB. Once the Arduino is programmed, it becomes a stand-alone device which does whatever you program it to as soon as you turn it on.

Making Sound with the Arduino

I had some ideas about how to make sound with an Arduino, but in the end I didn’t have to go through too much trial and error thanks to an article called Arduino Sound Part 2: Hello World, Created by David Fowler of uCHobby.com

In David’s article he shows how to make a very simple program and circuit that will turn an Arduino into a sound generator.

I copied David’s code into the Arduino IDE, with just one change; I wanted to use output pin 13 whereas David had written his code to work with output number 9.

Here’s the original code:

//Arduino Sound Hello World
//Created by David Fowler of uCHobby.com
//Define the I/O pin we will use for our sound output
#define SOUNDOUT_PIN 9

void setup(void){
//Set the sound out pin to output mode
pinMode(SOUNDOUT_PIN,OUTPUT);
}

void loop(void){
//Generate sound by toggling the I/O pin High and Low
//Generate a 1KHz tone. set the pin high for 500uS then
//low for 500uS to make the period 1ms or 1KHz.

//Set the pin high and delay for 1/2 a cycle of 1KHz, 500uS.
digitalWrite(SOUNDOUT_PIN,HIGH);
delayMicroseconds(500);

//Set the pin low and delay for 1/2 a cycle of 1KHz, 500uS.
digitalWrite(SOUNDOUT_PIN,LOW);
delayMicroseconds(500);
}

This much worked perfectly for the generation of a simple tone, but I wanted to add some user interaction, so I found this article about using a potentiometer on the Arduino website and made some changes to my code.

Here’s the finalized code modified to support an input from a potentiometer:

// Arduino Sound Hello World
// thanks to David Fowler of uCHobby.com for his informative article and
// the majority of this code
// see the original at :
// http://www.uchobby.com/index.php/2007/11/14/arduino-sound-part-2-hello-world/

// Define the I/O pin we will use for our sound output
#define SOUNDOUT_PIN 13

int sensorPin = 0; // the number of the input we're using for the potentiometer
int sensorValue = 0; // initializing the sensorValue, the value itself is arbitrary.
int pulseLength = 500; // this is the default pulseLength giving us a 1KHz tone

void setup(void){
pinMode(SOUNDOUT_PIN,OUTPUT);
}

void loop(void){
//Generate sound by toggling the I/O pin High and Low
//Generate a 1KHz tone. set the pin high for 500uS then
//low for 500uS to make the period 1ms or 1KHz.

//Set the pin high and delay for 1/2 a cycle of 1KHz, 500uS.
digitalWrite(SOUNDOUT_PIN,HIGH);
delayMicroseconds(pulseLength);

//Set the pin low and delay for 1/2 a cycle of 1KHz, 500uS.
digitalWrite(SOUNDOUT_PIN,LOW);
delayMicroseconds(pulseLength);

//Read the value of the potentiometer and reset the pulseLength
sensorValue = analogRead(sensorPin);
pulseLength = sensorValue*8; // multiplying input to get a lower tone.
}

That’s it!

This is my first crack at putting together a tutorial, and also my first hack at building with an Arduino, so I’m sure I’ve left stuff out, done stuff wrong, or deserve a general grilling. Please leave a comment and school me!

4 responses to “Exploring The Arduino”

  1. iffer says:

    So amazing!!! This is my favorite website now. Love the tutorials and all the interviews, especially the one with Vincent and his cat. =)

    Are you the Frederick who has a nice garage?

  2. Justin Alm says:

    Hey, sweet stuff. Love the bonus round.

  3. frederick says:

    Hi Iffer,

    Thanks for the comment. I only just noticed it now, months later. I’m pretty stoked about my garage, it’s true. Maybe one of these days I’ll do a studio-visit on my own self and get that up here. In the meantime you’ll just have to imagine the awesomeness of the “G-raj”.

    – F

  4. frederick says:

    Thanks Justin!