Hello everyone, I'm using the bounce example sketch slightly modified to turn off/on my desk lamp using a 5V Relay Module KY-019 a push button and Arduino Uno.
Push button has one leg connected to GND and the other leg connected to pin 2 using the pull up resistor. The relay is connected to 5V, GND and signal to pin 6.
The issue that I'm having is that sometimes I push the button and the led on the relay slightly dims and nothing happens. It seems like maybe is not receiving enough current? I then tested my circuit with the Debounce example sketch and it works perfectly. So I'm not sure what's going on.
/*
DESCRIPTION
====================
Simple example of the Bounce library that switches the debug LED when a button is pressed.
*/
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
#define BUTTON_PIN 2
#define LED_PIN 6
bool light_on = true;
// Instantiate a Bounce object
Bounce debouncer = Bounce();
void setup() {
// Setup the button with an internal pull-up :
pinMode(BUTTON_PIN, INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
debouncer.attach(BUTTON_PIN);
debouncer.interval(5); // interval in ms
//Setup the LED :
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// Update the Bounce instance :
debouncer.update();
// Get the updated value :
int value = debouncer.read();
// Turn on or off the LED as determined by the state :
if ( value == LOW ) {
light_on = !light_on;
digitalWrite(LED_PIN, light_on );
}
}
/*
Debounce
http://www.arduino.cc/en/Tutorial/Debounce
*/
// 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 = 6; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = HIGH; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == LOW) {
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}