Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Uno R3 Controlled LED Blink and Sleep Circuit

View and Edit Project In Cirkit DesignerImage of Arduino Uno R3 Controlled LED Blink and Sleep Circuit

Circuit Documentation

Summary of the Circuit

This circuit is designed to control two LEDs using an Arduino Uno R3 microcontroller. The LEDs can be turned on and off using rocker switches. The Arduino is also programmed to enter a low-power sleep mode when a button is pressed and to wake up from sleep mode when another button is pressed. The circuit includes resistors to limit the current through the LEDs, ensuring they operate safely.

Component List

Resistor

Rocker Switch

LED: Two Pin (red)

Arduino Uno R3

DC Power Source

Wiring Details

Resistor (330 Ohms)

Rocker Switch

LED: Two Pin (red)

Arduino Uno R3

DC Power Source

Documented Code

// These are the two libraries that are needed
#include <avr/interrupt.h>
#include <avr/sleep.h>

/* Here we set up our inputs and outputs. LEDs connected to pins 10 and 13 and pushbuttons attached to 2 and 12 */
int ledPin = 13;
int sleepPin = 12;
int interruptPin = 10;
int wakePin = 2;
// sleepStatus is set up to keep track of the button input on pin 12.
int sleepStatus = 0;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, OUTPUT);
  pinMode(sleepPin, INPUT_PULLUP);
  pinMode(wakePin, INPUT_PULLUP);

  // Enable an interrupt on pin 2 (digital pin 2)
  attachInterrupt(0, wakeUpNow, LOW);
}

void sleepNow()
{
  // Print message to serial monitor to let the user know board has gone to sleep
  Serial.println("going to sleep");
  // Delay to allow user to get the full message on the serial monitor before going to sleep
  delay(15);

  // Enables the sleep mode
  sleep_enable();

  // Enable the interrupt, so that if the button is pressed it doesn't interrupt the running program.
  attachInterrupt(0, wakeUpNow, LOW);

  // Set the sleep mode to power down
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);

  // Put the device to sleep
  sleep_mode();

  // Disable the sleep mode upon waking up
  sleep_disable();

  // Disable the interrupt on pin 2 so the wakeUpNow code will not be executed during normal run time
  detachInterrupt(0);

  // Wait 1 second so the user can notice the LED signaling the interrupt
  delay(1000);
  digitalWrite(interruptPin, LOW);
}

void wakeUpNow()  // This is the code that runs when the interrupt button is pressed and interrupts are enabled
{
  digitalWrite(interruptPin, HIGH);
}

void loop()
{
  // Turn the LED on
  digitalWrite(ledPin, HIGH);
  // Wait for a second
  delay(1000);
  // Turn the LED off
  digitalWrite(ledPin, LOW);
  // Wait for a second
  delay(1000);
  // Read the sleep pin status
  sleepStatus = digitalRead(sleepPin);

  // If button is pressed, run the sleepNow function
  if (sleepStatus == LOW) {
    sleepNow();
  }
}

This code is designed to control the LEDs and manage the sleep mode of the Arduino Uno R3. The setup() function initializes the pins and the interrupt. The loop() function turns the LED on and off and checks the status of the sleep pin. If the sleep pin is pressed, the sleepNow() function is called, which puts the Arduino into a power-down sleep mode. The wakeUpNow() function is the interrupt service routine that wakes up the Arduino from sleep mode.