The LED is lit!

Gloria Julien
5 min readJun 15, 2019

--

Using Arduino to light up an LED

This content is part of a collection of works completed during my studies as a UX/IxD Master’s student at Thomas Jefferson University.

Here, I have documented my progress while taking the Prototyping Interactions I course during Summer 2019. This part of the course uses Arduino.

Follow me on LinkedIn.

Project Goals

  1. Create a circuit that lights up the LED when the button is pushed. Subsequently, have it turn off if the button is not pressed.
  2. Create a circuit that flashes the light when the button is long pushed. Subsequently, turn the light off when the button is not pressed.
Our goal!

Part I: Creating the Button Circuit

Using the Arduino button tutorial, I was able to set up my circuit as shown below. It shows an input of 5V, running through a resistor to the button. Then, the signal reaching the LED from the button input.

Circuit setup

Code:

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Output:

Button push to turn on LED

Part II: Flashing Lights with Long Button Push

Next, I wanted to make the lights flash when the button is pushed down and held down. My goal was to simulate a laser beam shooting from the Magic School Bus (project) to shoot the oncoming smelly objects.

In this part, I’m going to combine what I’ve learned from the blink() tutorial to add a blinking effect to my code. If the button is detected as being on, then the LED should flash. I chose to set my delay between the on/off states of the LED to something short, in order to mimic a laser beam frequency coming out of the school bus.

Code:

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int elapsedTime = 500; // if the button is pushed for 500ms, then start blinkingint longPress = 0; // the number of seconds the button is pushed for// variables will change:
//unsigned long timeElasped; // variable for reading the pushbutton status
int buttonState = 0; // variable for reading the pushbutton status
unsigned long timePressed;
unsigned long currTime;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // turn LED on:
delay(50);
digitalWrite(ledPin, LOW); // turn LED off:
delay(50);
} else {
digitalWrite(ledPin, LOW); // turn LED off:
}
}

Output:

Flashing LED with long button press

Part III: Two Laser Beams

With a slight modification, I added another LED light to give our Magic School Bus an extra boost. I made pin 12 the location of my second LED and rewired the LEDs so they both make a connection to ground.

Modified Circuit and Paper MagicSchool Bus

Code:

I added a constant to read pin 12, our second LED light.

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int ledPin2 = 12;
const int elapsedTime = 500; // if the button is pushed for 500ms, then start blinkingint longPress = 0; // the number of seconds the button is pushed for// variables will change:
//unsigned long timeElasped; // variable for reading the pushbutton status
int buttonState = 0; // variable for reading the pushbutton status
unsigned long timePressed;
unsigned long currTime;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // turn LED on:
digitalWrite(ledPin2, HIGH); // turn LED on:
delay(50);
digitalWrite(ledPin, LOW); // turn LED off:
digitalWrite(ledPin2, LOW); // turn LED off:
delay(50);
} else {
digitalWrite(ledPin, LOW); // turn LED off:
digitalWrite(ledPin2, LOW); // turn LED off:
}
}

Final Output:

Success!

Thanks for reading!

--

--

Gloria Julien

I think a lot about people’s perceptions about life. Oh, and I’m a UX-er, who’s also turning into a Career Coach.