Back
Featured image of post Output Devices

Output Devices

Week 9

Output devices

Assignments

Our tasks for this week are:

  • Group assignment: Measure the power consumption of an output device
  • Individual assignment: Add an output device to a microcontroller board you’ve designed, and program it to do something

Group Assignment

Group documentation can be found here.

Determining the power consumption of an output device is accomplished by measuring the voltage and current across the component with a multimeter. Power = current & voltage, so the power consumption of any output device can be calculated by multiplying the measured voltage drop across the device by the current drawn while the device is in operation.

In the group assignment we did this both with a simple LED and with a simple motor, both in unloaded and loaded (close to stall, by preventing rotation with a hand) states.

Individual Assignment

This week I built off the cardioDev board I built last week.

The output device I measured was an RGB LED which I’d included in my cardioDev board. In response to pushing a button on my board, the RGB LED changed to a random color.

Datasheet for the RGB LED I used | PDF file

There are two types of RGB LEDs: common anode or common cathode. The main difference is that a common cathode is connected to ground, while the common anode is connected to VCC - this is something I took into account when I designed my board.

The surface mount RGB LED available in the lab has four pads: one pad each for the Red, Green, and Blue LEDs, and one pad for the common anode. Even though it’s one surface mount component, there are actually three little LEDs within the RGB LED, one Red, one Green, and one Blue. By controlling the amount of signal through each you can achieve any color you want.

The common anode is connected to VCC and the Red, Green, and Blue pads are each connected to individual pins of the microcontroller, through three resistors.

To figure out the value of the three resistors needed, I checked the datasheet to find the average forward current of the RGB LED is 20mA. The average forward voltage for operation of the the R, G, and B components are 2, 3.2, and 3.2 V respectively. We can calculate the voltage through each resistor as VCC - [Voltage of each LED component]= 5V - 2V for the R-LED and 5V - 3.2V for the G-LED and B-LED. Then I used ohm’s law, Voltage = current * resistance, to solve for the resistance needed for each of the RGB components of the RGB LED. I got a value of 150, 90, and 90 ohms for the R, G, and B pins respectively. However, since we didn’t have resistors of those exact values in the lab, I used a 220 ohm resistor for the R-LED and 100 ohm resistors for the G-LED and B-LED. (It’s safe to use higher resistance values but not lower, to prevent the LEDs from burning out.)

the Code

I programmed my board using the Quentorres board I produced in week 4

I used the Arduino IDE interface for programming the microcontroller. I wrote the program in collaboration with ChatGPT. Because the RGB LED has three pins, one to control each color, I set each of them as an OUTPUT. I defined the button pin as an INPUT. When the button is pushed I set each of the three RGB pins to a random value between 0-255 and write that color to the LED.

Download the file here

Or copy and paste from below:

/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/
const int ledPin = 6; 

// define the pin for the pushbutton
const int buttonPin = 7;

// define the pin for the trimmer/potentiometer
const int pot = 0;

// define the RGB LED pins
const int ledPinR = 8;
const int ledPinB = 9;
const int ledPinG = 10;

// Define the initial color values
int redValue = 255;
int greenValue = 0;
int blueValue = 0;

// define an initial trimmer/potentiometer value
int potValue = 0;

// Define a variable to store the button state
int buttonState = 0;
int lastButtonState = 0;


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(ledPin, OUTPUT);
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinB, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(buttonPin, INPUT);

}

// the loop function runs over and over again forever
void loop() {

// Read the state of the pushbutton
  buttonState = digitalRead(buttonPin);

  // Check if the button is pressed
  if (buttonState != lastButtonState && buttonState == HIGH) {
    // Change the color when the button is pressed
    changeColor();
  }

  // Save the current button state for the next iteration
  lastButtonState = buttonState;
}

void changeColor() {
  // Generate random color values
  redValue = random(256);
  greenValue = random(256);
  blueValue = random(256);

  // Write the color values to the RGB LED pins
  analogWrite(ledPinR, redValue);
  analogWrite(ledPinG, greenValue);
  analogWrite(ledPinB, blueValue);

  // Delay for debounce (optional)
  delay(100);
}

Hero shots

Video of the RGB LED cycling through random colors whenever the button in pushed

Reflections

I still feel intimidated by output devices such as motors but this week gave me confidence to interact with simple output devices such as the RGB LED.

Later on I also used an LED strip as an output device, controlled by a piezoelectrode input device when exploring how to detect heart rate. See more in input devices week

Built with Hugo | Using a personally adjusted version of Nadieh Bremer's website and the theme Stack