Skip to content

Week 09. Output Devices

Group Assignment:

  • Measure the power consumption of an output device.

Group Assignment Link

Some Takeaways:

  • Datasheets provide crucial technical specifications like voltage, current, and power consumption.
  • Understanding operating conditions ensures reliable performance and longevity.
  • Pinout diagrams and functions aid proper integration into circuits.
  • Timing characteristics helps control when and how fast the device responds.
  • Response times indicates how quickly the device reacts to input signals.

Individual Assignment:

  • Add an output device to a microcontroller board you’ve designed and program it to do something.

Disclaimer: All the Assignments and Documentations are done and written by me, however after writing it myself, some I pass it through chatGPT to fix my language and grammar.

This week for Output Devices, we need to add an output device to the micro controller board that we have designed and produced during Electronics Design Week.

Piezo Buzzer

image source

  • Piezo buzzer like a mini noisemaker that’s really light and doesn’t cost much. You can set it up to make all sorts of sounds, like beeps or alarms. Depending on how big it is and how you adjust it, it can make sounds that are either soft or super loud.

Code:

void setup() {
  pinMode(21, OUTPUT);
}
void loop() {
  tone(21, 261); //Middle C
  delay(1000);

  tone(21, 277); //C#
  delay(1000);

  tone(21, 294); //D
  delay(1000);

  tone(21, 311); //D#
  delay(1000);

  tone(21, 330); //E
  delay(1000);

  tone(21, 349); //F
  delay(1000);

  tone(21, 370); //F#
  delay(1000);

  tone(21, 392); //G
  delay(1000);

  tone(21, 415); //G#
  delay(1000);

  tone(21, 440); //A
  delay(1000);
}

  • After I wrote down code, I uploaded this code to my ESP32 mcu board. I set GPIO 21 as output pin. Each tone gives a different musical note, starting from middle C (261 Hz) and through a C major scale up to A (440 Hz).
  • Then the code gives a square wave of specified frequency on GPIO 21 using the tone() function.
  • And then each tone is played for 1 second with a delay of 1-second.

RGB LED

image from this source

  • RGB LED is consists of three LEDs thats put into one body: red, green, and blue. To get different colors, you make code to how bright should each of the three LEDs should shines.

Code:

//code reference from chatGPT
//Program to control RGB LED
//common cathode: gnd to common
//HIGH ON
//LOW OFF
int rled=5;
int gled=19;
int bled=21;
void setup() {
  pinMode(rled, 5);
    pinMode(gled, 19);
    pinMode(bled, 21);
}
void loop() {
  // rled on
  digitalWrite(rled, HIGH);
  digitalWrite(gled, LOW);
  digitalWrite(bled, LOW);
  delay(100);
  //gled ON
  digitalWrite(rled, LOW);
  digitalWrite(gled, HIGH);
  digitalWrite(bled, LOW);
  delay(100);
  //bled ON
  digitalWrite(rled, LOW);
  digitalWrite(gled, LOW);
  digitalWrite(bled, HIGH);
  delay(100);
  //red & green ON with 50% brightness & bled OFF
  analogWrite(rled,127);
  analogWrite(gled,127);
  analogWrite(bled,0);
  delay(100);
  //green & blue ON with 50% brightness, rled OFF
  analogWrite(rled,0);
  analogWrite(gled, 127);
  analogWrite(bled, 127);
  delay(100);
  //blue & red ON with 50%, gled OFF
  analogWrite(rled, 127);
  analogWrite(gled, 0);
  analogWrite(bled,127);
  delay(100);
}

  • Above code is for controlling RGB LED by putting ON and OFF the allocated pins for each color and with each color coming up for 0.1 second before changing to the next color.

MG90S Micro servo motor

image from this source

  • The TowerPro MG90S micro servo motor, with an operating voltage typically ranging from 4.8V to 6V (5V standard) delivers a stall torque of 1.8 kg/cm at 4.8V and up to 2.2 kg/cm at 6V.

Code:

//code edited from chatGPT
#include <ESP32Servo.h>
Servo myServo;
int servoPin = 21;
void setup(){
  myServo.attach(servoPin);
  Serial.begin(115200);
}
void loop(){
  if(Serial.available()){
    int angle = Serial.parseInt();
    myServo.write(angle);
  }
  delay(20);
}

  • After writing code, I compiled the code and uploaded it to the my board. The code facilitated servo movement up to 180 degrees based on the input provided via the serial monitor. Additionally, I adjusted the settings in the serial monitor to ensure that “No Line Ending” was selected.

Files of the Week

Codes(.ino) folder