Skip to content

Input Devices

Group Assignment:

  • Probe an input device(s)’s analog and digital signals
  • Document your work to the group work page and reflect on your individual page what you have learned

Individual Assignment:

  • Measure something : add a sensor to a microcontroller board that you have designed and read it.

Learning outcomes

  • Demonstrate workflows used in sensing something with input device(s) and MCU board.

As usual here is my schedule:

Group Assignment

We were instructed to use test instruments (multimeters, oscilloscopes, etc.) to observe the signal that a sensor was producing for this week’s group task. We made the decision to test an ultrasonic sensor and a sound sensor using an oscilloscope to view the signal.

MEASUREMENT WITH THE OSCILLOSCOPE

Firstly, here is a little information about an oscilloscope!!

An oscilloscope is a device used to visualize electrical signals. It converts these signals into visible waveforms, which are displayed on a screen. The oscilloscope consists of several components, including signal input probes, signal conditioning systems, vertical and horizontal.

SO let’s begin by writing the arduino code in Arduino IDE for the ultrasonic sensor!!!

Using Ultrasonic Sensor

Here is the code we used:

const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor

void setup() {
   Serial.begin(9600); // Starting Serial Terminal
}

void loop() {
   long duration, inches, cm;
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   delay(100);
}

long microsecondsToInches(long microseconds) {
   return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;
}const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor

void setup() {
   Serial.begin(9600); // Starting Serial Terminal
}

void loop() {
   long duration, inches, cm;
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   delay(100);
}

long microsecondsToInches(long microseconds) {
   return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;
}const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor

void setup() {
   Serial.begin(9600); // Starting Serial Terminal
}

void loop() {
   long duration, inches, cm;
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   delay(100);
}

long microsecondsToInches(long microseconds) {
   return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;
}const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor

void setup() {
   Serial.begin(9600); // Starting Serial Terminal
}

void loop() {
   long duration, inches, cm;
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   delay(100);
}

long microsecondsToInches(long microseconds) {
   return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;
}

Connections

Here is we connected our pins:

  • Echo - Digital pin 6
  • Trig - Digital pin 7
  • Gnd - Gnd
  • Vcc - Vcc

Now connect it to the oscilloscope whereby probes are connected to Gnd and Echo pins!!

So after a few tries of displaying the signal, here is how it looks:

And this is how it looks in the serial monitor!

Sound sensor

A sound sensor is a device that converts sound waves into an electrical signal that can be processed by an electronic circuit. They can be used to detect and measure the amplitude, frequency, and duration of sound waves. It has both analouge and digital output pins. We decided to check the analogue signal!

Setting up

Here is the connection we made:

Now for the code, we decided to use the analogue serial example code. To find it go to files -> examples -> Analogue -> AnalogueInOutSerial -> then change the pins according to your connections.

Here is the code:

// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);

  // print the results to the Serial Monitor:
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\t output = ");
  Serial.println(outputValue);

  // wait 2 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:
  delay(2);
}

So the sensor does not really detect the sound waves that efficiently so here is how the serial monitor and the osilloscope display.

Click here to view the group assignment!

Individual Assignment

Hero shot

So for this week, I am planing on making and using a capacitive touch sensor as I might actually integrate this sensor instead of a button for my final project!! I will alsobe trying out ultrasonic sensor!!

So let’s begin with touch sensor!

Capacitive Touch Sensor

A capacitive touch sensor operates based on the principle of capacitance. It comprises electrodes separated by a dielectric material. When a conductive object, like a human finger, comes close to or touches the sensor, it introduces a change in capacitance by altering the electric field between the electrodes. This change is detected by the sensor’s circuitry, typically by measuring the time taken for the sensor to charge and discharge. The sensor interprets these variations and triggers a response, such as activating a switch or inputting a command. It’s widely used in devices like smartphones, tablets, and touch-sensitive interfaces due to its sensitivity and responsiveness.

So now let’s begin by testing it on an arduino uno board. But before that it is important to install the required libraries.

Designing my board

I decided to use Kicad to design my board. Here is my schematic design in kicad!!

For connecting the wires in pcb, I recommend you all to set different tracks for easier navigation like this!

And now export the file: Go to file -> Export -> SVG -> and then select the path where you want to svae it.

Using Mod CE to set the toolpath.

In Mods right click -> Programs -> Open Program -> Roland (SRM-20 mill 2D PCB)-> Select png/svg

Then:

SO to add the save module do this:

Now do the same thing for the edge cut except for the mill type:

For milling, I am going to use Roland SRM 20 miling machine.

Set the setting like this:

Cutting!!

This is how it turned out before soldering:

FEW MOMENTS LATER!!

Setting up

Soldering

I used a small copper sheet for now and soldered a male jumper wire and 1 mega ohm resistor to it like this:

This is how it looks:

As I am using a led, it is important to use a resistor typically 1k ohms for the led too. After gathering all the components, connect the 1M OHM resistor to digital pin 2 and the other wire to digital pin 4. Similarly, for the led, connect 1 pin to the GND and the resistor to pin 8.

(SPOILER!! IT DID NOT WORK SO WHEN I TRIED TO USE COPPER PLTAES SO I ENDED UP USING THE TOUCH SENSOR BUTTON!!)

Library

For the capacitive touch sensor you will need this library:

CapacitiveSensor.h

I downloaded the zip file from this link like this:

  • Click here to download the zip file:

  • Then open Arduino IDE and go to sketch -> Include libraries -> Add zip files -> select the zip file we downloaded earlier:

After including the library, we can use the example code from here :

Testing

I decided to use an led to test the capacitive touch sensor and this is the code I used:

#include <CapacitiveSensor.h>

CapacitiveSensor Sensor = CapacitiveSensor(4, 2);
long val;
int pos;
#define led 8

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

void loop()
{

val = Sensor.capacitiveSensor(30);
Serial.println(val);
if (val >= 1000 && pos == 0)
{
digitalWrite(8, HIGH);
pos = 1;
delay(500);
}

else if (val >= 1000 && pos == 1)
{
digitalWrite(8, LOW);
pos = 0;
delay(500);
}

delay(10);
}

Explanation of the code:

Firstly, I initialised the library ‘CapacitiveSensor.h’ which will handle the capacitive touch sensing (will take in inputs from the sensor).

Then a ‘CapacitiveSensor’ object named Sensor is created. The object is initialised with 2 pins (Send Pin (Pin 4)- Sends electrical pulses and Receive Pin (Pin 2)- receives the pulses and measures the capacitance.)

Then a serial communication is initialised at 9600 baud to enable data monitoring via the serial monitor. Pin 8 is set as an output to control the LED

The ‘capacitiveSensor()’ function is called with a timeout parameter of 30 milliseconds. This function sends multiple electrical pulses through the send pin and measures the time it takes for the pulses to be received on the receive pin.

The measured capacitance value is printed to the serial monitor for debugging and monitoring.

And for the LED, if the ‘pos == 0’, the led turns on and if the ‘pos == 1’, the led turns off.

And here is the output:

Capacitive touch sensor button

After trying to make the diy capacitive touch sensor work for a long time, I decided to move on and work on this sensor for the week. So the Capacitive touch sensor button has 3 pins, namely; VCC, GND, and Signal pin. Due to this, it does not require any library making it easy to use and it was quite funn!!!

This is how it looks:

Led and touch sensor

So I started programming using an arduino uno board. AND here is the code!!

// Henry's Bench
// Capacitive Touch Sensor Tutorial

// When Sig Output is high, touch sensor is being pressed

#define ctsPin 2

// Pin for capactitive touch sensor

int ledPin = 8;

// pin for the LED

void setup()

{

Serial.begin(9600);

pinMode(ledPin, OUTPUT);

pinMode(ctsPin, INPUT);

}

void loop()

{

int ctsValue = digitalRead(ctsPin);

if (ctsValue == HIGH)

{

digitalWrite(ledPin, HIGH);

Serial.println("TOUCHED");

}

else{

digitalWrite(ledPin,LOW);

Serial.println("not touched");

}

delay(500);

}

Explanation:

Firstly, ‘cstPin’ (Capacitive touch sensor pin) is defined as pin 2 and ‘ledPin’ as Pin 8. After that the serial communication is set at 9600 baud as usual.

The ‘ledPin’ is set as an output to control the LED and ‘ctspin’ takes in input from the capacitive touch sensor.

So the logic here is that, if the sensor is touched (ctsValue == HIGH), the LED is turned on and “TOUCHED” is printed to the serial monitor. And if the sensor is not touched, the LED is turned off and “not touched” is printed to the serial monitor.

So this code allows you to control the led which is in Digial pin 8. Here is how the serial monitor should look after uploading the code:

And here is a video of me testing it!

After confirming that the sensor works perfectly fine, I decided to do it on the board I designed!! SO I used the same code but changed the signal and led pins according to my board which were pin 20 and 4 repectively.

Firstly select the board and ports!!

And Here is a image of the serial monitor since it’s not clear in the video :(((

Here is how it turned out!!

Led, buzzer and touch sensor

After trying out with a led, I got interested and used a buzzer. So the buzzer has 2 pins namely; GND and Signal pin. SO I edited the code and here is the code:

// Henry's Bench
// Capacitive Touch Sensor Tutorial
// Henry's Bench
// Capacitive Touch Sensor Tutorial

#define ctsPin 20
#define ledPin 4
#define buzzerPin 8 // Choose a digital pin for the buzzer

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as an output
  pinMode(ctsPin, INPUT);
}

void loop() {
  int ctsValue = digitalRead(ctsPin);

  if (ctsValue == HIGH) {
    digitalWrite(ledPin, HIGH);
    // Produce a longer and louder sound
    for (int i = 0; i < 1000; i++) { // Adjust the number of iterations for longer sound
      digitalWrite(buzzerPin, HIGH);
      delayMicroseconds(100); // Adjust this delay for desired frequency (higher value for lower frequency)
      digitalWrite(buzzerPin, LOW);
      delayMicroseconds(100); // Adjust this delay for desired frequency (higher value for lower frequency)
    }
    Serial.println("TOUCHED");
  } else {
    digitalWrite(ledPin, LOW);
    digitalWrite(buzzerPin, LOW); // Turn off the buzzer when not touched
    Serial.println("not touched");
  }

  delay(500);
}

This how the code looks !!

Explanation:

As before I defined the led and the capacitive touch sensor along with the ‘buzzerPin’ as Pin 8.

Here the LED and Buzzer acts as outputs and the capacitive touch sensor takes in inputs.

The conditional logic here is that if the sensor is touched, the led turns on with the buzzer producing a sound and prints “TOUCHED” to the serial monitor. Similarly, if the sensor is not touched, the led and buzzer are turned off and it prints “not touched” to the serial monitor!

After connecting the wires, here is the end result!!

Ultrasonic Sensor

Ultrasonic sensors use high-frequency sound waves to detect objects and measure distances. They’re widely used in industries like manufacturing and robotics due to their accuracy and non-contact operation. However, their performance can be affected by factors like temperature and object properties.

Now let’s start by installing the library required for the sensor which is the Afstandssensor!

Firstly open arduino ide, go to sketch -> Include libraries -> Manage libraries -> search for Afstandssensor -> install it!

NOW after including the library, go to files -> Examples -> Afstandssensor -> Simple:

Then change the pins according to your connection!

Like this:

// HC_SR04 - https://github.com/bjoernboeckle/HC_SR04
// Copyright © 2022, Björn Böckle
// MIT License

#include <Arduino.h>
#include <HC_SR04.h>

HC_SR04<3> sensor(5);   // sensor with echo and trigger pin

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

void loop() {
  sensor.startMeasure();
  Serial.println(sensor.getDist_cm());
  delay(1000);
}

Explnation:

This code is by Björn Böckle which comes along with the library. So this code basically initialises the serial monitor and the HC-SR04 ultrasonic sensor.

SO if the capacitance value (‘val’) is greater than or equal to 1000 and if the led is off (pos == 0), it turns teh led on and Vice-versa.

ANd here is the connection:

Now run the code!!

Here is how the serial monitor looks like:

And here is a video of me testing it!

THAT’S IT !!!

Reflection

This week, working with new sensors and libraries like CapactiveSensor and HC_SCR04 has taught me a lot of things. I was also able to apply conditional logics to control outputs such as leds and buzzers. After being able to contrl the sensors, I was really happy and this week was a really great experience as always! (Thankfully, I didnt come across any major issues!)

I want to acknowledge the help from AI, particularly OpenAI’s ChatGPT, for assisting me in understanding and writing the code for integrating and using different sensors. As for the documenting the code explanations, I rephrased them according to my understanding of the codes.

Compressions:

Imagemagick

I used image magick for image compression. Here are some commands I used for compression:

Ffmpeg

I used ffmpeg for video compression. Here are some commands I used:

  1. Basic Conversion:

Convert a video file to another format:

ffmpeg -i input.mp4 output.avi
  1. Changing Video Resolution:

Resize a video:

ffmpeg -i input.mp4 -vf scale=640:480 output.mp4
  1. Extracting Audio:

Extract audio from a video file:

ffmpeg -i input.mp4 -vn -c:a copy output.mp3

Click here see the new words I learnt this week!

FIles

THAT’S IT FOR THIS WEEK!!

HAVE A NICE DAY!


Last update: July 8, 2024