Double Pushbutton LED Circuit

Intro: Double Pushbutton LED Circuit

I'm going to try my utmost to sound like an old hand at programming and working with arduinos but, truth be told, in reality I was only introduced to these things a few days ago, hence my overwhelming joy at successfully creating and programming my own project so soon.

This project actually started out from a dare from an engineer friend of my parents that I was an intern for for a few days. I love challenges, there is something in being challenged that excites me and gives me and overwhelming sense of satisfaction when I succeed, and I hope you do too.

In this instructable, my first ever :D, I will show you how to connect the circuit for what I called my Mark One project, a circuit where two push-buttons control the brightness of the LED ( one will increase the brightness and the second will decrease it ) as well as how to program it and project your results on the serial monitor tool. I will show you exactly what you need as well as a step-by-step guide on how to build it. My project incorporates elements of the both the push-button- as well as the fade lesson on the instructables website and is heavily commented for my own convenience as well as yours. If your not yet familiarised with the programming language please do take the time, its well worth it.

Enjoy!

Step 1: Materials + Tools:

Materials:

1. An Arduino board ( I used a Mega 2560 )

2. A breadboard ( with atleast 60 rows )

3. One LED ( any colour, I used red )

4. Two standard pushbuttons

5. Five blue wires ( to indicate the negative current )

6. Three red wires ( to indicate the positive current )

7. One orange-, green- and yellow wire ( PWM control wires to differentiate between the 3 main components )

8. Four white wires ( to connect the circuit )

9. Three 10K ohm resistors ( Brown, Black, Orange, Gold )

Tools:

1. A laptop

2. The Arduino IDE app ( downloadable from the Microsoft Play store

OR

2. The Arduino create online website ( if that's what you prefer )

3. Nimble hands

4. Google ( instructables website helped me a lot )

5. Loads of patience ;D

Step 2: Set Up Your Analog Circuit:

The difference between Analog and Digital systems are that Analog systems code for different states such as different brightness's of an LED while Digital only codes for two states ( either ON or OFF ).

This circuit is an Analog one because the goal was to make the light change brightness, not just go on or off.

To set it up:

1. Take one blue ( for negative ) wire and plug it ( on the Arduino ) at either one of the GND ( ground ) ports at the power ports and connect it anywhere on breadboard in the rows next to the blue negative ground.

2. Take one red ( positive ) wire and plug it ( on the Arduino ) at the 5V port at the power ports and connect it to the a row on the breadboard adjacent to the red positive ground line [ Do familiarise yourself with how the breadboard's layout is set up and what rows are connected etc. Instructables and the Arduino website have very neat lessons to do just that ]

3. At the opposite end of the breadboard you use one blue and one red wire to connect the ground lines.

4. Then use the four white wires to connect the ground rows of both positive and negative to either side of row 30 of the middle ( they should bridge a small gap )

Step 3: Set Up the Push-buttons:

1. Now take your push-buttons and connect them so that it straddles the middle gap of the breadboard at any point ( I like to spead my stuff out a bit so I can more easily change and fix things.

2. Take two red wires and connect the right leg of the pushbuttons facing you with the positive ground line.

3. Take two blue wires and connect the blue negative ground line with with centre rows a few rows left of the left legs of the push-buttons, leaving space for the resistors.

4. Now connect the 10K resistors ( Brown, Black, Orange, Gold ) with one leg in the same centre row as the negative blue wire and the other leg in the same row as the left leg of the push-button

[ All of this is still connected on the one side of the divide in the middle of the breadboard except the buttons straddling the divide and the white-, red- and blue wires for the analog set-up )

5. Now take the yellow and green wire and connect the left leg on the opposite side of the centre divide with pins 9 and 11 [ NB: Analog only works on pin 3,5,6,9,10 and 11 ]. These wires are for communication with the Arduino.

Step 4: Set Up the LED:

1. Take the LED ( colour of your choice ) and place it on the side of the divide where most of your connections are.

2. Take one blue wire and connect the negative blue ground line with the same row as the short leg ( anode ) of the LED [ The circuit will only work if the negative blue line is connected to the right leg of the LED i.o.w. the anode.

3. Take another 10K ohm resistor and connect the row into which the cathode ( positive long leg of the LED ) is plugged in with a nearby row on the same side of the centre divide.

4. Now take the orange wire and connect the resistor with pin 3 on the Arduino

Your circuit is now complete, all that is left now is to program everything

Step 5: Programming the Circuit:

I'm a bit new to all this so please excuse me for only copying the code over and not creating a video...

Here is the code for the circuit:

// My (Altus Lourens) first own project:
// Created between 29 and 30th of June, 2018

// NB for myself: AnalogWrite only works on 3,5,6,9,10 and 11

// NB: Analog has to do with different states, Digital only works for on(HIGH) or off(LOW)

// LED will be output

// pushbuttons will be inputs

// pushButton1 will increase brightness with one fadeAmount with every press

// pushButton2 will decrease brightness with one fadeAmount with every press

// constants won't change, set PWM pin numbers

const int ledPin = 3; // number of the PWM LED pin

const int fadeAmount = 50; // change in brightness with every push of the button

const int buttonPin1 = 11;

const int buttonPin2 = 9; // number of the pushbutton pin

// variable that will change:

int brightness = 5; // starting brightness of the LED's

int buttonState1 = 0; // specify button beginning as LOW

int buttonState2 = 0; // specify button beginning as LOW

void setup() {

// put your setup code here, to run once:

// initialise the LED pin as output:

pinMode(ledPin, OUTPUT);

// initialise the pushbuttons as inputs:

pinMode(buttonPin1, INPUT);

pinMode(buttonPin2, INPUT);

Serial.begin(9600); // speed of communication in the circuit

}

// buttonPin1 will increase the brightness of the LED

// buttonPin2 will decrease the brighness of the LED

void loop() {

// put your main code here, to run repeatedly:

// set the brightness of pin 9:

analogWrite(ledPin, brightness); // brightness = 5, LED is on

brightness = constrain(brightness, 0, 255); // constrain brightness between 0 and 255

// read the state of the pushbutton value:

buttonState1 = digitalRead(buttonPin1);

buttonState2 = digitalRead(buttonPin2);

// check if the buttons have been pressed:

// if it is -> buttonState = HIGH:

// pushbutton1:

if (buttonState1 == HIGH) {

brightness = brightness + fadeAmount; // increase brightness:

analogWrite(ledPin, brightness + fadeAmount);

}

else { // brightness stays at same level and LED is still off:

analogWrite(ledPin, brightness );

}

// pushbutton2:

if (buttonState2 == HIGH) {

brightness = brightness - fadeAmount; // decrease brightness:

analogWrite(ledPin, brightness - fadeAmount);

}

else {

// brightness stays at same level, no change takes place:

analogWrite(ledPin, brightness);

}

Serial.print("brightness");

Serial.println(brightness); }

You can now adjust the speed of communication and play around with it a bit until you find a speed that works perfectly for the serial monitor tool [ the 9600 Baud is just a standard speed ]

Share

    Recommendations

    • Tiny Home Contest

      Tiny Home Contest
    • Metalworking Contest

      Metalworking Contest
    • Audio Contest 2018

      Audio Contest 2018

    Discussions