User Menu

Fade Chip’s eyes using Arduino (Preview)

This is a preview lesson. Please sign up for the course to access all lessons.

Using Pulse-Width Modulation (PWM) to control LEDs

When we finish this lesson you will be able to use pulse-width modulation (PWM) to make it appear that Chip’s eyes are slowly getting brighter and then dimmer. You might ask why all the fancy language to make Chip’s eyes fade? Good question.

Lets use the example of dimming a light bulb like we find in our home. When you turn the dimmer switch on the wall you are turing a variable resistor and as you increase the resistance, you decrease the voltage and reduce the energy available for the light bulb. More energy equals a brighter light bulb, less energy equals a dimmer lightbulb. Simple and effective.

LED’s are a little different.  Every LED has a rated voltage and at that voltage an LED is it’s brightest and most efficient.  We can dim an LED by reducing the voltage, but the color of the LED can be effected and the results are harder to accurately predict. A better and controlled method is to keep the voltage level constant and turn the LED “on and off” at a very high frequency. This is what PWM is essentially doing, it is controlling the amount of time an LED is at zero and at it’s rated voltage. The ratio of on time to off time determines how bright the LED appears. With Arduino we can send out a PWM that turns “on and off” thousands of times per second, so fast our eyes and brain fill in the gaps, giving us the illusions the LED is always on, just dimmer or brighter.

Sources and more reading:


What you will need for this lesson:

  • Chip
  • A Salduino UNO R3 or an Arduino Uno R3 (or any compatible platform)
  • USB Cable
  • Arduino IDE installed on your PC or MAC

Read the code below. Step through it line by line. What do you think it will do? What do you think each line means?

int rEye = 11;
int lEye = 10; 

int brightness = 0;
int fadeAmount = 5; 

void setup(){

pinMode(rEye, OUTPUT);
pinMode(lEye, OUTPUT); 


void loop(){

 analogWrite(rEye, brightness);
 analogWrite(lEye, brightness); 

 brightness = brightness + fadeAmount;

 if (brightness == 0 || brightness == 255) {
 fadeAmount = -fadeAmount ;

 delay (30); 


Have some ideas about what that code does? Great! Lets step through that code again, this time with some comments that explain what each section does. The “//” signifies a comment. A comment is ignored and not sent to the Arduino. Comments are there for us humans, to make code more readable.

// Chip has 3 important pins
// Ground (GND) -- plugged into to Arduino GND
// Right Eye LED (D11) --> plugged into to Arduino D11
// Left Eye LED (D10) --> plugged into to Arduino D10
// We need to define Chip's eyes so we can use them in our code

int rEye = 11; //define right eye as pin11 on the Arduino
int lEye = 10; //define right eye as pin10 on the Arduino

int brightness = 0; // how bright the Chip's Eye's will be
int fadeAmount = 5; // how many points to fade his eyes by

// Next we need to initialize the Arduino pins
// setup() only runs once when you power or reset an Arduino

void setup(){

pinMode(rEye, OUTPUT); //set right eye for output
pinMode(lEye, OUTPUT); //set left eye for output


//loop runs forever - this is our main code
void loop(){

 // set the brightness of Chip's Eyes:
 analogWrite(rEye, brightness);
 analogWrite(lEye, brightness); 

 // change the brightness for next time through the loop:
 brightness = brightness + fadeAmount;

 // reverse the direction of the fading at the ends of the fade:
 if (brightness == 0 || brightness == 255) {
 fadeAmount = -fadeAmount ;

 delay (30); //wait for 30 millisecons second. Delay is set 1/1000 of a second


Your Turn.

  • Plug Chip into your Arduino.
  • Connect the Arduino to your computer.
  • Launch the Arduino IDE.
  • Start a new sketch. On the Arduino menu go to FILE -> NEW
  • Copy the code from the example sketch above. Paste into your Arduino IDE sketch.
  • Save the sketch. File -> SAVE (name it something such as sketch_Chip_PWM)

You should have a screen that looks like this:

sketch_Chip_Blink | Arduino 1.0.5

Verify the Sketch – click on the check mark icon. This will verify that your code does not have any syntax or procedural errors. However, it does not verify that your code will do what you want. That is up to you.

Arduino Verify

If the code is ok, you will not see an errors. If there are any errors, double check that you did not miss anything when you copied it from the above example.

You are ready to upload the code from your computer to the Arduino.  Click the Upload Icon arrow, which is next to the verify button.

sketch_Chip_Blink Arduino upload

After the upload is successful, Chip should be blinking at you.  Congrats!

Now we can play. Try these challenges:

– What happens with if you change the brightness to larger or smaller values?

int brightness = 0;


– What happens with if you change the fade amount to larger or smaller values?

int fadeAmount = 5;

-After changing the fade amount, why do you think we not just use a value of 1 for the fade amount?
– Challenge: Can you make One of Chip’s eye dim while the other becomes brighter?

- Challenge: Make chip Blink one eye and fade the other.

Don’t forget to verify and upload your code after each change.
Play around, you can not break anything. You can always re-copy and upload the original sketch.

Access all the code for Chip

GitHub is a website where you can store, share, and manage code for projects. We use it store all the code for Chip’s lessons so that it is easy for you to download. If you have code you would like to contribute, please do so. You can access Chip’s Github Repository here

Lesson tags: Arduino, Chip, led, pulse-width-modulation, PWM
Back to: Programming Chip with Arduino