In this article, I’ll show you how to program the Arduino to control the on-board LED with a push button. I’ll also show you a program that changes the LED’s flashing rate depending on how much light hits a photoresistor. At the end of the tutorial, I’ll show you how to use the photoresistor to vary the pitch of sound output by a speaker. This tutorial is part three of a series on the basics of getting started with the Arduino. Part one of the series can be found here.

Controlling the Arduino’s LED with a Push Button

Each of the Arduino’s GPIO pins (General Purpose Input/Output) can be programmed to act as either inputs or outputs. As inputs, pins read electrical signals sent from other devices, and as outputs they send signals to other devices.

In this project, we’ll write a program that will define a pin as an input. Then we’ll connect a push button to it. When the button is pressed, the Arduino will send a signal to an output pin to make an LED turn on and off.

It’s pretty easy to do as you’ll see in the diagram and code below.

First, connect a 1K Ohm resistor and a push button to the Arduino like this:

Pin 2 LED Button

Now, enter this code into the Arduino IDE and upload it to the board:

const int ledPin = 13; 
const int inputPin = 2; 
void setup() {
pinMode(ledPin, OUTPUT); 
pinMode(inputPin, INPUT); 
}
void loop(){
int val = digitalRead(inputPin); 
if (val == HIGH) 
{
digitalWrite(ledPin, HIGH); 
}
else
{
digitalWrite(ledPin, LOW); 
}
}

The Arduino’s LED should light up when the push button is pressed, and turn off when the button is released.

How the Program Works

The code pinMode(ledPin, OUTPUT); on line 4 defines pin 13 as an output pin and gives it the name “ledPin“.

The code pinMode(inputPin, INPUT); on line 5 defines pin 2 as an input pin and gives it the name “inputPin“.

You can change the input and output pins to any other available GPIO pin by simply changing the pin numbers on lines 1 and 2 of the program above.

The digitalRead(inputPin) statement on line 8 tells the Arduino to listen to the inputPin (pin 2) for any signals that might be sent to it.

Line 9 tells the Arduino to listen for a HIGH signal at the inputPin, which will happen when the push button is pressed. If a HIGH signal is detected, line 11 will instruct the Arduino to send an output signal to the ledPin (pin 13), making the LED light up.

Lines 13 to 16 tell the Arduino to keep the signal at the ledPin LOW (no current flowing to the LED) if the signal at the inputPin is LOW. The signal at the inputPin will be LOW as long as the button is not pressed.

Controlling the Arduino’s LED with a Photoresistor

A photoresistor is a light sensitive resistor that changes resistance depending on how much light hits it. The resistance decreases with more light, and increases with less light.

We can use the Arduino to detect the resistance of the photoresistor and use that value to control the flashing rate of an LED. Start by connecting a photoresistor and a 1K Ohm resistor to the Arduino like this:

Photoresistor LED

Enter this code into the Arduino IDE and upload it to the Arduino:

const int ledPin = 13; 
const int sensorPin = 0; 

const int minDuration = 100; 
const int maxDuration = 1000; 

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

void loop()
{
 int rate = analogRead(sensorPin); 
 rate = map(rate, 200,800,minDuration, maxDuration); 
 digitalWrite(ledPin, HIGH); 
 delay(rate); 
 digitalWrite(ledPin, LOW); 
 delay(rate);
}

Now wave your hand over the photoresistor, and you should see that the LED flashes faster when your hand is covering the photoresistor.

How the Program Works

First we define the output pin (pin 13) and input pin (analog pin 0) on lines 1 and 2 with these two statements:

const int ledPin = 13;
const int sensorPin = 0;

We want each individual flash of the LED to be long when lots of light hits the photoresistor. When no light hits the photoresistor, we want the LED flashes to be short. We define the minimum and maximum duration of the flashes on lines 4 and 5. Line 4 sets the minimum duration (minDuration) at 100 ms, and line 5 sets the maximum duration (maxDuration) at 1000 ms.

Line 14 tells the Arduino to read the resistance of the photoresistor at the sensorPin (pin 0) and to give the detected value a name called “rate“.

The Arduino reads the resistance of the photoresistor and assigns it a value between 200 and 800. The lowest resistance is given a value of 200, and the maximum resistance is given a value of 800.

Line 15 says to define a range of resistance values (from 200 to 800) and “map” them to the range of minDuration and maxDuration values (from 100 ms to 1000 ms). That way, when the detected resistance has a value of 200, the rate value will correspond to the minDuration value of 100. When the detected resistance has a value of 800, the rate value will correspond to the maxDuration value of 1000 ms. All values of resistance from 200 to 800 will be mapped to duration values between 100 ms and 1000 ms.

The delay(rate) functions in the void loop() section first tell the Arduino to send a HIGH signal to the ledPin for the duration of the rate value.  Next, a LOW signal is sent to the ledPin for the duration of the rate value. This produces the on/off flashing effect.

Controlling Audio Pitch with a Photoresistor

This project is similar to the previous project where we used the photoresistor to control the rate of flashing of the Arduino’s LED. However now we’ll use the photoresistor to control the audio frequency emitted by a passive speaker. The only difference in the code here is that we will change the rate of the output signal.

First, connect a 1K Ohm resistor and a passive speaker to the Arduino like this:

Photoresistor Speaker

Then, enter this code into the Arduino IDE and upload it to the board:

const int ledPin = 13; 
const int sensorPin = 0; 
const int minDuration = 100; 
const int maxDuration = 1000; 

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

void loop()
{
 int sensorReading = analogRead(sensorPin); 
 int rate = map(sensorReading, 200,800,minDuration, maxDuration);
 rate = rate / 100; 
 digitalWrite(ledPin, HIGH); 
 delay(rate); 
 digitalWrite(ledPin, LOW); 
 delay(rate);
}

Now wave your hand over the photoresistor and notice how the pitch output by the speaker increases as the light hitting the photoresistor decreases.

That about wraps up this series on getting started with the Arduino. If you want to learn more, be sure to check out our other tutorials on projects like connecting an LCD, setting up an ultrasonic range finder, or using a humidity and temperature sensor with the Arduino.

Check out this project, and I’ll show you how to use a chip called the 555 timer to produce this same effect.

Feel free to leave a comment below if you need help setting the circuit or if you have any questions! And be sure to subscribe to get an email notification when we publish new tutorials…


Need an easy-to-use way to design circuits and layout PCBs?

Try EasyEDA, a free circuit design software that also offers low cost, high quality PCB manufacturing.