Skip to content

11. Input devices


Week assignment

  • This week’s assignment was to measure something by adding a sensor to a microcontroller board that I have designed and read it.

  • Probe an input device’s analog levels and digital signals (group).


Sensors

Basically a sensor acquires a physical quantity and converts it into a signal suitable for processing. Sensor: The input is a physical quantity and the output is electrical. Actuators: The input is electrical and output is a physical quantity.

Analog vs Digital Sensor

I wanted to compate a digital and an analog sensor so I decided to use the analog temperature sensor and digital the temperature sensor.

Before the test I read about the differences between analog an digital sensors and how they work here.

An analog signal is one that can take on any number of values, unlike a digital signal which has only two values: HIGH and LOW. However, they require an analog-to-digital converter (ADC) to read the output.

  • When value = 0, the signal is off.
  • When value = 255, the signal is on.

A digital sensor outputs a signal with discrete steps and are easier to use.

Temperature sensors

Analog temperature sensor

A temperature sensor basically senses temperature and converts it into output signals. It uses an NTC (negative temperature coefficient) termistor, which means that the resistance decreases with higher temperature.

I used an Arduino UNO board and an analog temperature sensor module for this test. The idea is to see how the value of A0 decrese when I touch the sensor. I’ll check this values on the serial monitor.

Thermistor_datasheet

Connection

I uploaded this code to the Arduino UNO and I used this library: LiquidCrystal_l2C I found the library here. Just downloading the ZIP.

  1. To install the library I opened the Arduino IDE > Sketch > Include Library > Add ZIP Library.
  2. Find the library inside Sensor Kit V2.0 for Arduino and click on LiquidCrystal_l2C to open it.
  3. Then, I got the message “Library added to your libraries” and I checked that the library appears in Tools > Include library

Then I just copy this code into the Arduino IDE, selected the board and the port, verified that everything was fine and clicked to upload it.

//Analog Temperature Sensor
const int digitalPin = 7;     // Analog Temperature Sensor pin D0 to pin7
int analogPin = A0;           // Analog Temperature Sensor pin A0 to pin A0
const int ledPin =  13;      // pin 13 built-in LED light

// variables will change:
boolean Dstate = 0;          // variable for reading status of D0
int Astate = 0;            // variable for reading status of A0
void setup() 
{
  pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:     
  pinMode(digitalPin, INPUT);  // initialize Analog Temperature Sensor D0 pin as an input
  Serial.begin(9600); // initialize serial communications at 9600 bps 

}

void loop()
{
  Astate = analogRead(analogPin);  // read Analog Temperature Sensor A0 value (set point)
  Dstate = digitalRead(digitalPin);  // read state of Analog Temperature Sensor D0
  Serial.print("D0: ");
  Serial.println(Dstate);//print the value of D0
  Serial.print("A0:");
  Serial.println(Astate);//print the value of A0
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (Dstate == HIGH)    // check if Analog Temperature Sensor D0 is HIGH
  {     
    // turn LED on:    
    digitalWrite(ledPin, LOW);  
  } 
  else 
  {
    // turn LED off:
    digitalWrite(ledPin, HIGH); 
  }
  delay(1000);                // controls speed of Analog Temperature Sensor and Serial Monitor display rate
}
Then, I opened the serial monitor.

  • When touching the sensor, which means that temperature is higher, A0 decreases and D0 turns from 0 to 1. We can also see that the LED on pin 13 turned off.

Holding the sensor

  • When releasing the sensor, which means that temperature decreases, A0 increases, and D0 turns from 1 to 0 again, causing the LED light on pin 13 to turn on.

Not holding the sensor

Digital temperature sensor

Sensor DS18B20,requires only one pin for a two-way communication with a microprocessor.

I used this code and added some additional libraries following the same steps as for the Analog Sensor.

Connection digital

/****************************************************
name:Digital Temperature Sensor-ds18b20
function:you can see the value of current temperature displayed on the LCD.
****************************************************/
//Email:support@sunfounder.com
//website:www.sunfounder.com

/****************************************/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
#define ONE_WIRE_BUS 7 //ds18b20 module attach to pin7
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

void setup(void)
{

  // start serial port
  Serial.begin(9600);
  sensors.begin(); // initialize the bus
  lcd.init(); //initialize the lcd
  lcd.backlight(); //turn on the backlight
}
void loop(void)
{ 
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  lcd.setCursor(0, 0);
  lcd.print("TemC: "); //print "Tem: " on lcd1602
  lcd.print(sensors.getTempCByIndex(0));//print the temperature on lcd1602
  Serial.print("Tem: ");
  Serial.print(sensors.getTempCByIndex(0));
  Serial.println(" C");
  lcd.print(char(223));//print the unit" ℃ "
  lcd.print("C");
  lcd.setCursor(0, 1);
  lcd.print("TemF: ");
  lcd.print(1.8*sensors.getTempCByIndex(0) + 32.0);//print the temperature on lcd1602
  lcd.print(char(223));//print the unit" ℉ "
  lcd.print(" F");
  //Serial.print("Tem: ");
  //Serial.print(1.8*sensors.getTempCByIndex(0) + 32.0);
  //Serial.println(" F");
  //Serial.println("");
  //Serial.print("Temperature for the device 1 (index 0) is: ");
  //Serial.println(sensors.getTempCByIndex(0));  //print the temperature on serial monitor
}
In this case, as I used an LCD I was able to visualize the temperature before opening the serial monitor.

  • When touching the sensor the temperature increases. This is much simpler than the analog sensor output.

Holding the sensor digital

  • When releasing the sensor, the temperature decreases.

Not holding the sensor digital

Probe an input device(s)’s analog and digital signals

measuring

Digital temperature sensor

DS18B20 is 1-Wire digital temperature sensor from Maxim IC. Reports degrees in Celsius with 9 to 12-bit precision, from -55 to 125 (+/-0.5). Each sensor has a unique 64-Bit Serial number etched into it - allows for a huge number of sensors to be used on one data bus.

Working Voltage: 3V~5.5V

I connected the sensor to an Arduino UNO to the 5V pin. So, when measuring voltage with the multimeter it was 5.11V. I also measure amperage: 78.1 mA

Analog temperature sensor

An analog temperature sensor module that consists of an NTC thermistor, resistor, capacitor in a circuit. The NTC thermistor is very sensitive to ambient temperature and hence widely used to detect the temperature, with a wide range of temperature measurement. The threshold of temperature detection can be adjusted by rotating the potentiometer. When the temperature exceeds the threshold, D0 outputs low and the corresponding LED will light up.

Working voltage: 3.3V-5V

I connected the sensor to an Arduino UNO to the 5V pin. So, when measuring voltage with the multimeter it was 5.10V. I also measure amperage: 91.9 mA

Accelerometer

The MPU6050 IMU has both 3-Axis accelerometer and 3-Axis gyroscope integrated on a single chip. This combination of sensors is frequently referred to as an IMU, or “Inertial Measurement Unit”. The number of of sensor inputs in an IMU are referred to as “DOF” (Degrees of Freedom), so a chip with a 3-axis gyroscope and a 3-axis accelerometer would be a 6-DOF IMU because of its 6 outputs, or the 3 accelerometer outputs and the 3 gyroscope outputs.

The accelerometer can measure gravitational acceleration along the 3 axes. Using trigonometry math we can calculate the angle at which the sensor is positioned.

The gyroscope measures rate of change of the angular position over time, along the X, Y and Z axis. The outputs of the gyroscope are in degrees per second. We can get the angle integrating the velocity.

I followed this page to try the accelerometer.

Connection to Arduino

For this test I used the I2C protocol for communication with the Arduino. So, I only needed two wires for the connection, plus the two wires for powering it.

Connection accelerometer

I looked at the Data sheet of the sensor.

I read this document to understand how the acceleromerter works.

I uploaded this code to the Arduino to try the acelerometer Code that I took from MPU-6050 Accelerometer + Gyro section in Arduino playground website.

moving accelerometer

moving accelerometer

The board

First attempt

After doing some research and asking the instructors I decided to use as an example the 6 axis accelerometer+gyroscope board on FabAcademy inputs week site. But instead of using the LSM6DS33 sensor I used the MPU-6050 sensor.

I started to create my schematic using Kicad and following the steps I documented on week 07. Everything was looking fine, so I saved my file as an .svg then imported it into Inkscape and created my interior and traces .png files. Then, I opened modsproject and generated the files for the milling machine. However, I did not double check if the position of the pins matched the senor’s pins. They did not. Anyway, I did not realized about this until I finished soldering my board and pinned the sensor.

board fail

Board components

Components Description Quantity
ATTINY1614 8-bit tinyAVR® 1-Series Microcontroller 1
UPDI connector Female connector pinout UPDI 1
1UF Multilayer Ceramic Capacitors MLCC - SMD/SMT 50V 0.1uF 1
Male pin h 1x6 Male Pin Header 1
MPU-6050 MEMS accelerometer and a MEMS gyro in a single chip 1

board fail

oops

Find the fail-files here:

Final board

I redesigned the board to match the accelerometer’s pins. Then did all the same process again.

These are the .pngs that I got into modsproject to get the files for the milling machine.

interior and traces

Here you can see the new board and the difference with the old one.

new old

This is the new board, It ended being smaller than expected, which is great. As the sensor board has an LED it was not necessary to add one to the main board.

new board

I had an issue with the connection. I thought that I was connection RX-RX and TX-TX but I mistakenly inverted them. Then, when I went through connecting everything with Josep it worked fine.

connection

To test it, I used the same code that I was using for the Arduino uno but Carla reccommended me to use the option Upload Using a Programmer. It worked.

code

I need to clean up this code for the final project but for now, it worked as it is and it was sensing correclty.

working board

Find the final files here:


Last update: July 6, 2021