loading

Show info
Amy Roberts

Amy Roberts is a product designer currently based in Austin, TX.

Published March 28, 2016

Arduino: Integrated Modules

Light Dimmer Switch

Description

I used an LED along with a trim pot (potentiometer, an analog input sensor) to simulate a dimmer switch to control light. Although the LED is a digital output actuator, I used the PWM (Pulse Width Modulation) trick to make the LED behave like an analog input so I could use the trim pot to dim the light.

Process

I began by going through the LED fading tutorial on the Arduino site so I could better understand how the PWM trick worked. I then searched for a tutorial using the trim pot, and I found one that was already using the LED dimming trick. I used the code from this tutorial and tweaked the placement of the components on my breadboard to fit everything. I used Fritzing to create a diagram of my build and generate a schematic of my circuit.

IMG_6343-editIMG_6345Light_dimmer_schemLight_dimmer_bb

Reflections

The concept of PWM seemed complicated to me at first, so I was surprised at how little code was needed for it. After going through the tutorial on the Arduino site, I still didn’t feel like I understood the concept very well, but the second tutorial I found on toptechboy.com did a very good job of explaining the values and voltages. I was impressed by how smooth the transition was between dimming and brightening the light. One challenge I encountered was I wanted to find a way to slow down the serial monitor output so it could be read more easily, but I could not put a delay directly in the loop because it would interfere with the smoothness of the light dimming.

Code

int potPin= A0;  //Declare potPin to be analog pin A0
int LEDPin= 9;  // Declare LEDPin to be arduino pin 9
int readValue;  // Use this variable to read Potentiometer
int writeValue; // Use this variable for writing to LED
 
void setup() {
  pinMode(potPin, INPUT);  //set potPin to be an input
  pinMode(LEDPin, OUTPUT); //set LEDPin to be an OUTPUT
  Serial.begin(9600);      // turn on Serial Port
}
 
void loop() {
  
 readValue = analogRead(potPin);  //Read the voltage on the Potentiometer
 writeValue = (255./1023.) * readValue; //Calculate Write Value for LED
 analogWrite(LEDPin, writeValue);      //Write to the LED
 Serial.print(“You are writing a value of “);  //for debugging print your values
 Serial.println(writeValue); 
}

Dim LED with Potentiometer: http://www.toptechboy.com/arduino/lesson-11-arduino-circuit-to-dim-led-with-potentiometer/

Serial Monitor Output

The serial monitor output the values that were being written to the LED, controlling the brightness of the light. I manipulated these values by turning the potentiometer.

Screenshot 2016-02-28 17.00.20

Door Bell

Description

I used a button along with a piezo buzzer to create door bell prototype. Using a button library allowed me to differentiate between a button push and button hold, and I applied a different effect to each state.

Process

I began by going through the button tutorial on the Arduino site. Once I finished that, I added the piezo buzzer, and with the help of some code I found in an Arduino forum, I got the buzzer to sound when the button was pressed. To add a button holding capability, I downloaded the button library from my class slides on Canvas and merged some of that code with mine. I tweaked the frequency of the buzzer to create different tones and added a sequence of tones that play whenever the button is held down. I used Fritzing to create a diagram of my build and generate a schematic of my circuit.

IMG_6335-editIMG_6339Piezo_button_schemPiezo_button_bb

Reflections

I was surprised by how easy it was for me to merge the button code from the slides with the code I already had, but I successfully got it to work on the first try. I wasn’t sure how to output the changing value of the piezo buzzer in the serial monitor, so instead I just printed the note after each tone. For my button pressed state, I wanted to play part of a scale, and it was a little difficult tweaking the buzzer frequencies so the notes would be the correct pitch. I enjoyed creating this build because I could be creative with the sound output.

Code

#include 

// buzzer connected on pin 9
const int Buzzer1 = 9;

// declare a button connected on pin 2
Button butt (2);

void setup()
{
  // housekeeping
  Serial.begin(9600);
  Serial.println(“starting door bell”);
  pinMode(Buzzer1, OUTPUT); 
}

void loop()
{
  int action; // number code indicating what
  happened with the button

  // check the action status of the button
  action = butt.checkButtonAction();

  // manipulate buzzer according to button action
  if (action == Button::PRESSED) {
    Serial.print(action);
    Serial.println(“: pressed”);
    tone(Buzzer1,400,200);
    Serial.println(“\t G note”);

  } else if (action == Button::HELD) {
    Serial.print(action);
    Serial.println(“: held”);
    tone(Buzzer1,400,200);
    Serial.println(“\t G note”);
    delay(200);
    tone(Buzzer1,455,200);
    Serial.println(“\t A note”);
    delay(200);
    tone(Buzzer1,510,200);
    Serial.println(“\t B note”);
    delay(200);
    tone(Buzzer1,550,200);
    Serial.println(“\t C note”);
    delay(200);
    tone(Buzzer1,610,200);
    Serial.println(“\t D note”);
    delay(200);
    tone(Buzzer1,550,200);
    Serial.println(“\t C note”);
    delay(200);
    tone(Buzzer1,510,200);
    Serial.println(“\t B note”);
    delay(200);
    tone(Buzzer1,455,200);
    Serial.println(“\t A note”);
    delay(200);
  } else {
    // nothing happened with the button, so do
    nothing
  }
}

Sources:
Andy’s Button Code Library, Week 8 Slides on Canvas
Arduino Button Tutorial: https://www.arduino.cc/en/Tutorial/Button
Arduino Forum: http://forum.arduino.cc/index.php?topic=118185.0

Serial Monitor Output

The serial monitor output the button states (pressed or held) along with the tones the buzzer was making. I indented the tones to make it easier to read.

Screenshot 2016-02-28 15.33.25

amy