Back
Featured image of post Input Devices

Input Devices

Week 11

Input Devices

Assignments

Our tasks for this week are:

  • Group assignment: Probe an input device’s analog levels and digital signals
  • Individual assignment: Measure something: add a sensor to a microcontroller board that you have designed and read it

Group Assignment

A link to our group documentation of several input devices (a button and a potentiometer) is here.

We used an oscilloscope and a multimeter to measure and visualize the changes in voltage signal when using an input device. We did this with both a digital input (a button, where the input is a binary; high or low, 1 or 0) and an analog input (a potentiometer, where the value/range goes from 0-4095 (assuming a 12-bit ADC resolution)).

Individual Assignment

Input 1: Potentiometer

I used the cardio dev board I designed and fabricated in Week 8 to measure the readings from my little trimmer on the serial monitor.

The input was using a little screwdriver to physically rotate the trimmer that’s directly wired onto my board.

Code (Arduino IDE):

I used the Arduino IDE to read the signal from the trimmer when I rotated it with the screw driver.

const int pot = 0;       //the potentiometer/trimmer is on pin 0 of my board

void setup() {
  pinMode(pot, INPUT); 
  serial.begin(115200);

}

void loop() {
  int potValue = analogRead(pot);
  serial.println(potValue);
  delay(100);
}

Video

Rotating the trimmer on my board with a small screwdriver to seeing the value output on the serial monitor change

Troubleshooting

In the first tests there was a short that was occurring when I turned the trimmer. But with Adai’s help (thanks Adai!) we located the issue and after a quick and careful re-solder (did I mention how small that little trimmer is? I probably had too much solder on it originally) we were able to successfully get readings in the serial monitor!

Input 2: IR HR detecting module

Since I’d like to detect heart rate in my final project and use that to program the lights, I wanted to play around with heart rate detection during inputs week.

I found a little Arduino HR detection sensing module called the KY-039 in the lab.

After a quick search I found some code through a website called Electropeak with a tutorial for using this HR sensor.

Code (Arduino IDE):

/*
  Made on Jan 16, 2021
  By MehranMaleki @ Electropeak
  Home
*/

void setup() {
  pinMode(3, INPUT);    //analog pin, PA7
  Serial.begin(9600);
}

void loop() {
  float pulse;
  int sum = 0;
  for (int i = 0; i < 20; i++)
    sum += analogRead(3);
  pulse = sum / 20.00;
  Serial.println(pulse);
  delay(100);
}

Video

Testing the signal captured by the Arduino HR sensing module (using IR)

Input 3: Piezoelectrode

I also connected a simple piezoelectrode to my dev cardio board to test how well it detected heart rate via a fingertip.

The piezoelectrode has two wires; the black wire should connect to ground, and the red/orange wire to a pin.

Link to the Instructables tutorial I found on using a piezoelectrode to detect and visualize heartbeat.

Code (Arduino IDE):


int pin = A0;         // use whatever pin the piezoelectrode is connected to; in my case it is pin A0 on the Attiny1614 board I designed

void setup() {
  
  Serial.begin(9600);
  
}

void loop() {

Serial.println(analogRead(pin));
  delay(500);

}

Video

Measuring signals from the fingertip and visualizing as a graph on the serial plotter

Bonus: Piezoelectrode input, Neopixel strand output

After successfully reading signal from the piezoelectrode and plotting them on the serial plotter, we (thanks Josep!) connected a Neopixel LED strip as an output.

Code

#include <Adafruit_NeoPixel.h>

int pin = 1;
int LED = 6;
int previous;
int red = 0;

// Which pin on the Arduino is connected to the NeoPixels?
#define PIN 3  // On Trinket or Gemma, suggest changing this to 1

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 60  // Popular NeoPixel ring size

// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals. Note that for older NeoPixel
// strips you might need to change the third parameter -- see the
// strandtest example for more information on possible values.
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

#define DELAYVAL 500  // Time (in milliseconds) to pause between pixels

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);

  pixels.begin();  // INITIALIZE NeoPixel strip object (REQUIRED)
}

void loop() {
  int lecture = analogRead(pin);
  Serial.println(lecture);
  red = map(lecture, 0, 80, 0, 100);
  red = constrain(red, 0, 100);
  if ((lecture < previous) && (previous > 45)) {
    digitalWrite(LED, HIGH);
    //delay(200);
  } else {
    digitalWrite(LED, LOW);
  }
  previous = lecture;
  
  //pixels.clear();  // Set all pixel colors to 'off'
  pixels.fill(pixels.Color(red,0,0),0, 60);
  pixels.show();  // Send the updated pixel colors to the hardware.
  
  delay(100);

}

Video

Measuring signals from the fingertip and programming the neopixel strand to light up in response

Reflections

After trying two methods of heart rate detection (IR sensor vs piezoelectic/vibration sensing), I actually preferred the piezoelectrode! I wasn’t able to get a great signal out of the Arduino HR sensing module.

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