Skip to content

Week 5 - Input and Output Devices

This Week’s Accomplishments:

Testing Out Input & Output Devices.

  • Learned about input and output devices and coded them into my Arduino.

  • Tested out different sensors and connected them to the prototype of my final project.


Introduction

This week I learned that input and output devices are essential for interacting with the physical world using an Arduino. For instance, you can use a push-button as an input to control an LED as an output. A potentiometer can adjust the brightness of the LED or the angle of a servo motor. A photoresistor could be used to detect when it gets dark and then activate a buzzer or another form of alert. Similarly, a temperature sensor can trigger a relay to turn on a fan if the temperature exceeds a certain limit. Each device serves a unique purpose and can be programmed to perform a wide range of tasks when connected to an Arduino board.

Here is a comparison table of some of the devices I was introduced to in my FabLab class:

Type   Device Example Use
Input Push-button Used to start or stop a process
Input Switch Toggle between two states
Input Potentiometer Adjust levels like volume or brightness
Input Photoresistor Measure light intensity
Input Temperature Sensor Monitor temperature changes
Output LED Indicate status or alerts
Output Buzzer Emit sound as an alert
Output Servo Motor Precise control of movement
Output LCD Display Show information or readings
Output Relay Control higher power circuits

I tested out various objects to be prepared to use them for my final project. I followed these steps to test my electronics:

1.Connected the Hardware.

2.Connected one side of the button to GPIO pin 5.

3.Connected the other side of the button to ground.

4.Sat Up the microcontroller.

5.Connected my ESP32 to my computer via USB.

6.Opened the Thonny IDE and uploaded the codes.

7.Ran the script from the IDE.

8.Monitored the Output.

Input Devices

Buzzer

I first started out with an easy output device, a buzzer. So I tried to use the basic coding method I learned the previous week and came up with the following code. I used my Thonny python app to run it.

Horn

Moving on to another output device, I picked up the horn and tried to get it to play musical instruments. So I searched the web for a nice code to utilize and adjust and found a basic musical tone for the ABCs:

Then I tried to play the happy birthday song:

After that I did the theme of Super Mario :) got the code I used from ai here. The code to play the “Super Mario” theme on a horn. This example assumes you’re using a microcontroller ESP32 with a horn connected to one of the GPIO pins.

LED

To operate the LED light, I googled a MicroPython code example to control a WS2812 RGB LED (also known as NeoPixel) using the microcontroller. This example will light up the LED with different colors.

1.First, I had my neopixel library installed, you can install it using the following command in the MicroPython REPL.

import upip
upip.install('micropython-neopixel’)

2.This code initialized the NeoPixel on pin 5 which is what I had on and cycles through different colors (red, green, blue, yellow, cyan, magenta, and white) with a 1-second delay between each color:

import machine
import neopixel
import time

#Define the pin connected to the NeoPixel
pin = machine.Pin(5)  # Change to the correct pin number
num_leds = 1  # Number of LEDs in the strip

#Initialize the NeoPixel
np = neopixel.NeoPixel(pin, num_leds)

#Function to set the color of the LED
def set_color(r, g, b):
    np[0] = (r, g, b)
    np.write()

#Main loop to cycle through colors
while True:
    set_color(255, 0, 0)  # Red
    time.sleep(1)
    set_color(0, 255, 0)  # Green
    time.sleep(1)
    set_color(0, 0, 255)  # Blue
    time.sleep(1)

Output Devices

Push Button & a Fan Motor

For an input device, I picked out the button. I connected one side of the button to the Arduino Kids pin (pin 5 again) and the other side to ground. Used a 130DC fan motor connected to pin 18. The fan will turn on when the button is pressed and turn off when the button is released. You can download the code here. The script will print “Button Pressed - Fan On” when the button is pressed and “Button Released - Fan Off” when the button is released, every 0.1 seconds.

Now to make the fan keep working even after I release the button, I used a flag to track the fan’s state. The fan will turn on when I press the button and stay on until I press the button again to turn it off. The script will print “Fan On” when the button is pressed and “Fan Off” when the button is pressed again, every 0.1 seconds. Here’s my code.

Accelerometer

For my second input device, We were given our electric materials to utilize them for our final project. So my team took part in dividing each material on us where we tried to get familiar with them and learn how to use them. I took the opportunity and got the Adafruit MPU6050. Mind you I have zero knowledge about electronics and how to run them.. So I googled the name of my Adafruit and followed the steps they had on their official website here. Unlike the previous devices I tried above because I used the Arduino Kids IoT. But here I used the big kids Arduino :), the real stuff! Arduino Uno R3 with an adafruit.

I followed the steps from the official website:

1.I connected my Arduino Uno to the laptop and did the basic steps to get my laptop to recognize the Arduino from what I learned in week 4.

2.I took the code to program my Adafruit MPU6050 from the library on Arduino IDE app. sketch> include library> manage libraries> wrote my Adafruit name> install> install all.

3.I uploaded the code it wrote for me and watched my arduino do the magic for me.

Here’s the Adafruit MPU6050 Code I Used:

// Basic demo for accelerometer readings from Adafruit MPU6050

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;
void setup(void) {
  Serial.begin(115200);
  while (!Serial)
    delay(10); // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("Adafruit MPU6050 test!");

  // Try to initialize!
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }
  Serial.println("MPU6050 Found!");

  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  Serial.print("Accelerometer range set to: ");
  switch (mpu.getAccelerometerRange()) {
  case MPU6050_RANGE_2_G:
    Serial.println("+-2G");
    break;
  case MPU6050_RANGE_4_G:
    Serial.println("+-4G");
    break;
  case MPU6050_RANGE_8_G:
    Serial.println("+-8G");
    break;
  case MPU6050_RANGE_16_G:
    Serial.println("+-16G");
    break;
  }
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  Serial.print("Gyro range set to: ");
  switch (mpu.getGyroRange()) {
  case MPU6050_RANGE_250_DEG:
    Serial.println("+- 250 deg/s");
    break;
  case MPU6050_RANGE_500_DEG:
    Serial.println("+- 500 deg/s");
    break;
  case MPU6050_RANGE_1000_DEG:
    Serial.println("+- 1000 deg/s");
    break;
  case MPU6050_RANGE_2000_DEG:
    Serial.println("+- 2000 deg/s");
    break;
  }
  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
  Serial.print("Filter bandwidth set to: ");
  switch (mpu.getFilterBandwidth()) {
  case MPU6050_BAND_260_HZ:
    Serial.println("260 Hz");
    break;
  case MPU6050_BAND_184_HZ:
    Serial.println("184 Hz");
    break;
  case MPU6050_BAND_94_HZ:
    Serial.println("94 Hz");
    break;
  case MPU6050_BAND_44_HZ:
    Serial.println("44 Hz");
    break;
  case MPU6050_BAND_21_HZ:
    Serial.println("21 Hz");
    break;
  case MPU6050_BAND_10_HZ:
    Serial.println("10 Hz");
    break;
  case MPU6050_BAND_5_HZ:
    Serial.println("5 Hz");
    break;
  }

  Serial.println("");
  delay(100);
}

void loop() {

  /* Get new sensor events with the readings */
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  /* Print out the values */
  Serial.print("Acceleration X: ");
  Serial.print(a.acceleration.x);
  Serial.print(", Y: ");
  Serial.print(a.acceleration.y);
  Serial.print(", Z: ");
  Serial.print(a.acceleration.z);
  Serial.println(" m/s^2");

  Serial.print("Rotation X: ");
  Serial.print(g.gyro.x);
  Serial.print(", Y: ");
  Serial.print(g.gyro.y);
  Serial.print(", Z: ");
  Serial.print(g.gyro.z);
  Serial.println(" rad/s");

  Serial.print("Temperature: ");
  Serial.print(temp.temperature);
  Serial.println(" degC");

  Serial.println("");
  delay(500);
}

4.Then I clicked on the serial monitor icon above to allow me to view my adafruit readings, and it successfully showed me the acceleration and rotation of the axis x,y, and z, in addition to the temperature values in celsius.

5.Final Result:


My Week in a Summary

This week, each component, from the buzzer to the accelerometer, was a lesson in electronics, programming, and the magic of turning ideas into reality. With every line of code, I not only instructed my Arduino but also reinforced my understanding of how software controls hardware, creating a symphony of interactions that was both intellectually fulfilling and fun.


Last update: September 4, 2024