Home‎ > ‎Unit 08‎ > ‎

Analog Write

In order to do this lesson you'll have to open the Arduino interface again and change the Firmata that is on your Arduino board. Unit 5 has instructions in case you forgot how to do this. You need: File->Examples->Firmata->SimpleAnalogFirmata. StandardFirmata seems to have problems executing analogWrite. Don't forget to make sure your board and port are selected in the tools menu. When you're done with this unit you can put back the StandardFirmata.

So far we've turned on and off LEDs, but some times we want them to be only half on. You can't do that with digital write. If you look at your Arduino, you'll notice 6 of your digital pins have PWM printed next to them. This stands for Pulse Width Modulation. Basically we can use these pins to make the LEDs dimmer. What we're really doing is flashing them very rapidly all the way on and all the way off. When they are dimmer they spend more time off than on. Our eyes simply see them as dimmer, but do not perceive the flashing.

Analog write can have values from 0 to 255. What's with these weird numbers? 255 for analogWrite and 1023 for analogRead? Where do these come from? They come from the underlying digital nature of computer. Watch the video below for more details.

After you've gotten SimpleAnalogFirmata onto your Arduino wire up an LED with resistor to pin 9, then try the following code:

import processing.serial.*; //Import the serial library into your sketch
import cc.arduino.*;        //Import the Arduino-Firmata library into your sketch

Arduino arduino;      //Create an instance of Arduino named arduino (can be any name) 

int ledPin = 9;  

void setup() {
  size(100, 100);    
  arduino = new Arduino(this, Arduino.list()[0], 57600);  //defines arduino our board and sets the communication rate

void draw()
  for (int level=0; level<255; level+=1)
  arduino.analogWrite(ledPin, level);

  • Make the LED fade on and then fade off
  • Fade more than one LED at the same time (note only the PWM pins)
After you've played with fading LEDs a bit dig through your kit for the 4 legged LED. This is really tree LEDs in one component. There is a red, green, and blue led. The fourth leg is the negative lead of the LED. In this case it is the longest of the four legs. So, the second one from the left in my picture.

Now, a word of caution here. Each of the long legs should get one of your 330 ohm resistors. Please do not bend the legs around too much, lest you snap them off. The red LEDs in your kits only cost about 3-5 cents each. This RGB LED costs about $2. Hook up a circuit like the one pictured below (click the picture to enlarge):

The resistors are attached to pins 9,10, and 11. Your first task will be to figure out which pin is which color. Once you've done this write a program to fade in and out different colors. Also set your sketch background() to be the same color as the LED.

In order to get best results you should make a diffuser.  One way to make the color mixing work better is to diffuse the light, in a light box. You can make a light box out of plain paper, scissors and some tape. Just make a paper box and cut a hole in it. Fill the box with tissue paper. The tissue acts as a diffuser, helping the light mix nicely. You may find ou don't need a full sheet of kleenex.

Subpages (1): Color Match Game