Skip to content

7. Input & Output device

This week I worked on input and output devices. Input devices are used to send data to a computer or microcontroller. They allow users to provide information and commands to the system. Examples for inputs: microphone, mouse and keyboard.

Output devices receive data from a computer or microcontroller and present it to the user or another system. They convert electronic signals into perceptible forms. Examples for output: monitor, printer, speakers.

Output device

I used Input and Output in one application

I tested controlling multiple outputs with a microcontroller, using a button to activate them. I programmed it with Python through Thonny. The components involved were:

Component
Microcontroller
Buzzer
LED
RGB LED
Button
Fan
Wires
from machine import Pin, Timer
import time

# Pin definitions
button_pin = 27
led_pin = 26
rgb_pins = [12, 13, 14]
motor_pins = [2]
buzzer_pins = [17, 18, 19]

# Setup the button as an input with pull-up resistor
button = Pin(button_pin, Pin.IN, Pin.PULL_UP)

# Setup the LED as an output
led = Pin(led_pin, Pin.OUT)

# Setup RGB LEDs as outputs
rgb_leds = [Pin(pin, Pin.OUT) for pin in rgb_pins]

# Setup motor as outputs
motor = [Pin(pin, Pin.OUT) for pin in motor_pins]

# Setup buzzer as outputs
buzzer = [Pin(pin, Pin.OUT) for pin in buzzer_pins]

# RGB LED colors (common cathode configuration, 0 means LED is on)
COLORS = [
    (1, 1, 1),   # off (all pins high)
    (0, 1, 1),   # red (R pin low)
    (1, 0, 1),   # green (G pin low)
    (1, 1, 0),   # blue (B pin low)
    (0, 0, 1),   # yellow (R and G pins low)
    (1, 0, 0),   # cyan (G and B pins low)
    (0, 1, 0),   # purple (R and B pins low)
    (0, 0, 0)    # white (all pins low)
]

# Initialize state
current_color_index = 0

# Timer for color change every 2 seconds
color_timer = Timer(-1)

# Function to change RGB LED color
def change_color(timer):
    global current_color_index
    # Turn off current color
    set_rgb_led(COLORS[current_color_index])
    current_color_index = (current_color_index + 1) % len(COLORS)
    # Turn on new color
    set_rgb_led(COLORS[current_color_index])

# Function to set RGB LED color
def set_rgb_led(rgb_state):
    for pin, state in zip(rgb_pins, rgb_state):
        rgb_leds[pin - 12].value(state)

# Initialize RGB LED to off state
set_rgb_led(COLORS[current_color_index])

# Start timer to change color every 2 seconds
color_timer.init(period=2000, mode=Timer.PERIODIC, callback=change_color)

# Main loop
while True:
    # Check if the button is pressed (button is active low due to pull-up)
    if button.value() == 0:  # Button is pressed
        # Turn on the LED
        led.on()

        # Activate motor
        for pin in motor:
            pin.on()

        # Activate buzzer
        for pin in buzzer:
            pin.on()

        # Wait for 2 seconds (change RGB LEDs during this time)
        time.sleep(2)

        # Turn off the motor and buzzer after 2 seconds
        for pin in motor:
            pin.off()
        for pin in buzzer:
            pin.off()

    else:
        # Turn off the LED if the button is not pressed
        led.off()

    # Add a small delay to debounce and prevent rapid toggling
    time.sleep(0.1)

The Results:

Intput device

After that, i went to work on my project using MAX30100

I used this code using Arduino IDE usng C lan

#include <Wire.h>
#include "MAX30105.h"
#include "heartRate.h"

// Create an instance of the MAX30105 class to interact with the sensor
MAX30105 particleSensor;

// Define the size of the rates array for averaging BPM; can be adjusted for smoother results
const byte RATE_SIZE = 4; // Increase this for more averaging. 4 is a good starting point.
byte rates[RATE_SIZE]; // Array to store heart rate readings for averaging
byte rateSpot = 0; // Index for inserting the next heart rate reading into the array
long lastBeat = 0; // Timestamp of the last detected beat, used to calculate BPM

float beatsPerMinute; // Calculated heart rate in beats per minute
int beatAvg; // Average heart rate after processing multiple readings

void setup() {
  Serial.begin(115200); // Start serial communication at 115200 baud rate
  Serial.println("Initializing...");

  // Attempt to initialize the MAX30105 sensor. Check for a successful connection and report.
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) { // Start communication using fast I2C speed
    Serial.println("MAX30102 was not found. Please check wiring/power. ");
    while (1); // Infinite loop to halt further execution if sensor is not found
  }
  Serial.println("Place your index finger on the sensor with steady pressure.");

  particleSensor.setup(); // Configure sensor with default settings for heart rate monitoring
  particleSensor.setPulseAmplitudeRed(0x0A); // Set the red LED pulse amplitude (intensity) to a low value as an indicator
  particleSensor.setPulseAmplitudeGreen(0); // Turn off the green LED as it's not used here
}

void loop() {
  long irValue = particleSensor.getIR(); // Read the infrared value from the sensor

  if (checkForBeat(irValue) == true) { // Check if a heart beat is detected
    long delta = millis() - lastBeat; // Calculate the time between the current and last beat
    lastBeat = millis(); // Update lastBeat to the current time

    beatsPerMinute = 60 / (delta / 1000.0); // Calculate BPM

    // Ensure BPM is within a reasonable range before updating the rates array
    if (beatsPerMinute < 255 && beatsPerMinute > 20) {
      rates[rateSpot++] = (byte)beatsPerMinute; // Store this reading in the rates array
      rateSpot %= RATE_SIZE; // Wrap the rateSpot index to keep it within the bounds of the rates array

      // Compute the average of stored heart rates to smooth out the BPM
      beatAvg = 0;
      for (byte x = 0 ; x < RATE_SIZE ; x++)
        beatAvg += rates[x];
      beatAvg /= RATE_SIZE;
    }
  }

  // Output the current IR value, BPM, and averaged BPM to the serial monitor
  Serial.print("IR=");
  Serial.print(irValue);
  Serial.print(", BPM=");
  Serial.print(beatsPerMinute);
  Serial.print(", Avg BPM=");
  Serial.print(beatAvg);

  // Check if the sensor reading suggests that no finger is placed on the sensor
  if (irValue < 50000)
    Serial.print(" No finger?");

  Serial.println();
}

Here is the Results

This week, I found the assignments easy, likely due to their relevance to my major in Electronic Engineering. Given how easily I completed the tasks, I decided to assist my colleagues. I helped them with coding the ESP32 and also supported them in programming their sensors for their main project.


Last update: September 10, 2024