Friday, May 16, 2014

Getting started with Arduino - Simple Outputs

I'm going to start a small Arduino introduction series. I think it's important that people understand what's happening exactly when they create their first blinky program. The three things that I'm going to cover in this post is:
1. Turning an LED on
2. Blinking an LED on and off
3. Fading an LED for loop
We're also going to talk about how to use the IDE briefly, just so you can follow along better.

These three tasks are relatively simple, but understanding these will let you control thousands of other devices, not just LEDs!

To create a new sketch, all you do is open up the IDE, which can be downloaded at Arduino.cc. Once opened there should be nothing except for an empty area. Every Arduino program needs two functions to compile. Those functions are void setup() and void loop(). Those can be seen in the picture below.

1
2
3
4
5
6
7
8
9
void setup() {


}

void loop() {


}

The setup() function only runs once on the Arduino. It's often used to set pin modes as either inputs or outputs, and can be used to initialize variables at startup.

The loop() function runs forever, until your board loses power, or you program a stopping point. It runs very very fast, and a majority of your code goes in here. 

Now I want to explain what the pinMode() command does. pinMode() tells the Arduino what to set each pin as, either an input or output. pinMode has two parameters. The first parameter tells the Arduino which pin you're talking to, and the second parameter defines it as an input or output. Look at the picture below to see it being used. The pinMode() is also usually found in the setup() function.

1
2
3
4
5
6
7
8
9
void setup() {
  pinMode(3, OUTPUT); //Setting pin 3 as an output on the Arduino
  pinMode(8, INPUT); //Setting pin 8 as an input on the Arduino
}

void loop() {


}

digitalWrite() is how we set an OUTPUT pin high or low, meaning 5 volts or 0 volts. digitalWrite() has two parameters. The first one is which Arduino pin you're talking to, and the second is HIGH or LOW. To blink an LED at a steady pace it would be done like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
void setup() {
  pinMode(, OUTPUT); //Setting pin 3 as an output on the Arduino
}

void loop() {
 digitalWrite(3, HIGH);
 delay(500);
 digitalWrite(3, LOW);
 delay(500);
 
}

Above I use a delay() function. It only has one paramter, and it's in integer value in milliseconds. If you were to type
delay(500); 
The Arduino would pause what it was doing for 500 milliseconds and keep doing the command above it until the 500 milliseconds has passed.        

Another incredibly useful command would be analogWrite(). analogWrite() lets your vary the brightness of an LED or the speed of a motor. It uses something called pulse width modulation which does exactly what the name is. It varies the width of the HIGH LOW transitions to make it appear the voltage is changing, when in reality we're only changing the time that the pin is HIGH or LOW really fast. The parameters for analogWrite are the pin number, and a value between 0-255. 255 is a duty cycle of 100%, 127 would be 50%, and 0 would be 0%. It is very important to use a pin on the Arduino that is compatible with PWM. Look up the specs for your Arduino and use one of the pins that is useable with PWM.

To set an LED at half brightness it would look something like this:

1
2
3
4
5
6
7
void setup() {
  pinMode(3, OUTPUT);
}

void loop() {
  analogWrite(3, 127);
}


Now to fade an LED we would use a for loop. The for loop would need to control a variable to go from 0 - 255. When doing the the LED's brightness would ramp up, and with use of a second for loop, we can ramp it back down. The two loops would look something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
void setup() {
  pinMode(3, OUTPUT); //Setting pin 3 as an output on the Arduino
}

void loop() {
 for(int i = 0; i <= 255; i++) {
   analogWrite(3, i);
   delay(10);
 }
 for(int j = 255; j >= 0; j--) {
   analogWrite(3, j);
   delay(10);
 }
}
The reason we use a 10 millisecond delay in the LED fading is so it doesn't happen so fast, and it looks better to the eyes!

In the for loop I have a counter 'i'. It starts at 0, and every 10 milliseconds it increments by 1 (that's what the i++ does). It then writes the value of 'i' to the second parameter of analogWrite and the LED then shows that brightness. Once the first for loop is completed, it moves on to the second one, which is just the opposite. It starts the LED off at full brightness, and brings it back down.

Another really cool and important topic is the use of variables. Variables make code much more readable and easier to debug when you're having problems. All you need to do is create a variable, set a value to it, and use it in your program instead of hardcoding in the pin numbers. An example is below. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
int led = 3;

void setup() {
  pinMode(led, OUTPUT); //Setting pin 3 as an output on the Arduino
}

void loop() {
 for(int i = 0; i <= 255; i++) {
   analogWrite(led, i);
   delay(10);
 }
 for(int j = 255; j >= 0; j--) {
   analogWrite(led, j);
   delay(10);
 }
}


Notice how I removed the '3' in the analogWrite functions and replaced them with led. It makes the program look a little bit nicer I think.

Thanks everyone for reading! Next time we'll go over input devices, like switches, potentiometers, and LDR's (light dependent resistors).



Thursday, May 1, 2014

How to Create an Audio Amplifier with the LM386 OP AMP

This post isn't really anything Arduino related, but still a really cool project. I created an audio amplifier that you can connect to your iPod or whatever else you play music with and listen to it on a speaker.

In it's current state it's mono, but by adding a second speaker and second operational amplifier you can easily turn this into a stereo sound system.

I would highly recommend looking at the data sheet before building this. It shows you typical applications and other notes you would need if you were to design this from scratch. LM386 Datasheet

The parts needed for this are:
LM386 OP AMP x1
100uF Capacitor x1 [C2]
10uF Capacitor x1 [C1]
10 Ohm resistor x1 [R2]
1K Ohm resistor x1 [R1]
9 Volt Battery x1
10K ohm Potentiometer x1 [R6]
Speaker x1 (for mono)
9 Volt Battery snap x1
Audio Jack x1


The schematic for this is pretty simple, and fun to make. I think it's kinda nice making something that doesn't involve programming or micro controllers. I created the schematic using Eagle.


You can change the gain by adding a capacitor between pins 1 and 8. You can make the gain variable if you put a resistor in series with it. You can see that I did this at labels C1 and R1. I would reccomend you add a potentiometer instead of the resistor R1 so you can change the gain on the spot. If you don't put anything between pins 1 and 8, the built in 1.35K ohm resistor would set a gain of 20dB.

Capacitor C2 is used to help reduce noise to the speaker. It helps to remove constant DC voltage and only allows the audio waves to pass through.

If you wanted to make this stereo, you would need to repeat the circuit to be the same, except you would share the potentiometer that controls the gain between the two, and connect their grounds together. Inside of an audio cable there are 3 wires. Black for Ground, and the two other colors are for each audio channel. Connect one channel to one of the OP AMPS, and the other channel to the second one.

The next step is to solder this onto perfboard and put it in a nice enclosure. 

Here are some pictures of the one I made breadboarded up. 






Now here's some pictures of it being used with an oscilloscope so you can actually see the gain. 
It's hard to see the original signal a little bit but it's the bright small wave in the very center. The outside wave is actually the smaller signal amplified. Looks like it's amplifying very well! The gain might be set a little too high though, you can see in some spots how instead of a nice sinusoidal wave we get flatness at the top. That's because it's amplifying too much and is exceeding the OP AMPS power limits. This could be solved by using a 12 volt battery or greater compared to our 9 volt battery.

And here's just another view.

I hope you guys enjoyed this little project! My next couple of posts are going to be Intro to Arduino tutorials that I'm going to use for a school club I'm teaching. Those should be done fairly soon!