Getting started with Arduino? Awesome! It’s a fantastic platform for learning electronics and programming. One of the first things many beginners do is learn how to control things based on inputs. This simple program, let’s call it “Switch1”, does exactly that: it reads the state of a switch and turns an LED on or off accordingly.
It might seem basic, but understanding this code is a crucial first step. It introduces core concepts you’ll use in almost every Arduino project. Let’s dive in and see how it works!
What Does This Code Do?
At its heart, this program performs a straightforward task:
- Checks: Is a switch connected to a specific pin (now pin 2!) pressed or not?
- Acts: If the switch is pressed, it turns on an LED connected to another pin. If the switch is not pressed, it turns the LED off.
Simple, right? Now let’s look at the updated code line by line.
/*
Switch1 Program (Using Pin 2 for Switch)
*/
int switchPin = 2; // The switch is connected to digital pin 2
int ledPin = 13; // Often the built-in LED on many Arduino boards
void setup()
{
pinMode(switchPin, INPUT); // Set pin 2 as an input
pinMode(ledPin, OUTPUT); // Set the LED pin (13) as an output
}
void loop()
{
// Read the state of the switch on pin 2
if (digitalRead(switchPin) == HIGH)
{
// If the switch is HIGH (often meaning pressed), turn the LED on
digitalWrite(ledPin, HIGH);
}
else
{
// Otherwise (the switch is LOW), turn the LED off
digitalWrite(ledPin, LOW);
}
}
Breaking Down the Code
Let’s dissect the program structure and commands with the updated pin number.
1. Variable Declarations
int switchPin = 2;
int ledPin = 13;
int switchPin = 2;
: We declare an integer variable namedswitchPin
and assign it the value2
. This means we now plan to connect our switch to digital pin 2 on the Arduino.int ledPin = 13;
: This remains the same. We declareledPin
and assign it the value13
, telling the Arduino we’ll connect our LED to digital pin 13. (Remember, Pin 13 often has a built-in LED on boards like the Uno).
Using variables like this makes your code much easier to read and modify. If you decide to use different pins later, you only need to change these lines, just like we did!
2. The setup()
Function
void setup()
{
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void setup()
: This function runs only once when the Arduino board powers up or is reset. It’s used for initialization tasks.pinMode(switchPin, INPUT);
: This line configures the pin assigned toswitchPin
(which is now pin 2) as an INPUT. The Arduino will listen to the voltage level on this pin.pinMode(ledPin, OUTPUT);
: This configures the pin assigned toledPin
(pin 13) as an OUTPUT. The Arduino will control the voltage level on this pin.
3. The loop()
Function
void loop()
{
if (digitalRead(switchPin) == HIGH)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
}
void loop()
: This function runs continuously over and over again aftersetup()
finishes.digitalRead(switchPin)
: This command reads the current state of theswitchPin
(pin 2). It returns eitherHIGH
(usually 5 volts) orLOW
(usually 0 volts).if (digitalRead(switchPin) == HIGH)
: This is a conditional statement. It checks if the value read from pin 2 is equal (==
) toHIGH
. Important Wiring Note: As before, for this specific code to work as intended (HIGH when pressed), you typically need to wire the switch with a pull-down resistor connecting pin 2 to Ground normally, and the switch connecting pin 2 to 5V when pressed. (The alternative usingINPUT_PULLUP
still applies).digitalWrite(ledPin, HIGH);
: If theif
condition is true (pin 2 is HIGH), this command sets theledPin
(pin 13) toHIGH
, turning the LED ON.else
: If theif
condition is false (pin 2 is LOW), the code inside theelse
block is executed.digitalWrite(ledPin, LOW);
: This command sets theledPin
(pin 13) toLOW
, turning the LED OFF.
How it Works in Practice (Wiring to Pin 2!)
- Power On: The Arduino starts, runs
setup()
, configuring pin 2 as input and pin 13 as output. - Loop Starts: The
loop()
function begins. - Switch Check:
digitalRead(switchPin)
checks the voltage on pin 2.- Switch Not Pressed (with pull-down resistor): Pin 2 is connected to Ground through the resistor, so
digitalRead()
returnsLOW
. Theif
condition is false. - Switch Pressed (with pull-down resistor): Pressing the switch connects pin 2 to 5V, overriding the pull-down resistor.
digitalRead()
returnsHIGH
. Theif
condition is true.
- Switch Not Pressed (with pull-down resistor): Pin 2 is connected to Ground through the resistor, so
- LED Control:
- If the condition was true (switch pressed),
digitalWrite(ledPin, HIGH)
turns the LED ON. - If the condition was false (switch not pressed),
digitalWrite(ledPin, LOW)
turns the LED OFF.
- If the condition was true (switch pressed),
- Repeat: The
loop()
function immediately starts again from the top, constantly checking the switch state on pin 2.
Why is This Code Important?
(This section remains the same as the core concepts haven’t changed, just the pin number)
- Digital Input/Output: It’s the foundation of reading simple sensors (like buttons or switches) and controlling simple actuators (like LEDs or relays).
- Core Functions: Introduces fundamental Arduino functions:
pinMode()
,digitalRead()
,digitalWrite()
. - Program Structure: Demonstrates the essential
setup()
andloop()
structure of every Arduino sketch. - Conditional Logic: Uses the basic
if...else
statement, a cornerstone of programming.
Taking it Further
(This section also remains relevant, just remember switchPin
now refers to pin 2)
- Internal Pull-up: Modify the setup:
pinMode(switchPin, INPUT_PULLUP);
(whereswitchPin
is 2). This simplifies wiring (switch connects pin 2 directly to Ground). Remember, it inverts the logic: the pin readsLOW
when pressed. You’d need to change theif
condition toif (digitalRead(switchPin) == LOW)
. - Toggle: Change the code so pressing the switch toggles the LED state.
- Debouncing: Consider debouncing techniques for more reliable switch readings.
Conclusion
This simple “Switch1” program, now using pin 2 for the switch, is a fantastic starting point for interacting with the physical world using your Arduino. By reading an input (the switch on pin 2) and controlling an output (the LED on pin 13), you’ve reinforced the essentials of digital control.
Your Turn!
- Wire up your switch to pin 2 and the LED to pin 13 (don’t forget resistors!).
- Upload this updated code to your Arduino.
- Test it out!
- Keep experimenting! Try the internal pull-up resistor on pin 2 or other modifications.
Let us know how it goes! Happy making!