11. Input devices

This week I worked on discovering different inputs.

Assignment:

   individual assignment:
      measure something: add a sensor to a microcontroller board
      that you have designed and read it
   group assignment:
      probe an input device's analog levels and digital signals

Group assignement

Here is the link to the goup page. This week I probed different inputs to measure analog and digital signals. All of the following input were supplied with a 5V current.

Joystick

To understand how a joystick is working, I use the oscilloscope. The joystick receive a 5V current, and send the value of the potential meter. It is a position sensor. I measured with the oscilloscope the value in X.

The middle position of the joystick output a middle current. I send a 5V and the joystick in the middle state send back around 2.5V. When I move the potential meter in one way or in another way, the signal goes up or down, depending on the position, to go to 0V from 5V.

Reed switch

In the sensor kit for Arduino we have a reed switch. It is working with a magnet. There are two blades inside and when I approach the magnet the green light on the sensor turn on. The sensor is supplied in 5V.

When there is no magnet coming in send a signal of 5V, and when there is a magnet it comes to 0V. I reached to have a state in between depending on the distance of the magnet with the sensor.

Linear Hall

The linear hall sensor is working also with a magnet but it can read the side of the magnet. On the sensor there is a digital output or analog output.

First I measured with the analog and we can see a range of 2.5V for this sensor. If the magnet is on one side it send 1.25V and on the other side it send upper, 3.75V. The sensor send 2.5V without the magnet.

Then I plugged it with the digital output, and here it works with only one kind of sending signal, magnet (5V) or no magnet (0V).

Touch

I used also a touch sensor. I wired it both digital and analog.

The analog result was rather not precise. As we can see there is many variation on the output signal. When I don’t touch it is 0V, but when I touch it’s a lot of oscillation to reach 5V.

In digital output, the signal is more clear, touch is 5V, no touch is 0V.

Sonar

I finally want to see the probe with the sonar. Here is the wiring was little bit more complex.

The sonar needs 5V to work. The trig pin is the output, it sends a signal to the sonar to send the ultrasound. The signal send to the sonar needs to be more than the half of 5V, 2.5V to trigger the ultrasound wave. That is why I used a signal sender with a squared signal at around 3V. It sends 6 pulse by minute.

I measured it to be sure that it is not more than 5V to not spoil the sonar.

Then I plugged the other wire of the oscilloscope to the echo pin to see the signal received. On the oscilloscope, in yellow it is the signal send to the sonar, and in blue it is the signal received by it. The more the obstacle of the ultrasound wave is close, the more the thickness of the blue square will be small.

Arduino Uno Inputs

To discover the inputs, I first used an Arduino Uno board with different inputs. Because of my final project, I tried the sensor for motion and movement. I used VSCode to program the Arduino Uno because I like this IDE, with the syntax highlighting, and autocompletion. Moreover, it works pretty well with Arduino board.

Joystick

First I plugged the joystick on the Arduino board. I already used it during the machine week.

Here is the wiring I made.

And for the code I searched on internet and I found this website on how to use a joystick with Arduino. I took one variable for X value, one variable for Y value and a last one for the press button of the joystick.

#include <Arduino.h>

// https://www.aranacorp.com/fr/utilisation-dun-joystick-avec-arduino/

int valJoyX = 0;
int valJoyY = 0;
int BtnJoy = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(2, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  valJoyX = analogRead(A0);
  valJoyY = analogRead(A1);
  Serial.print("X = "); 
  Serial.println(valJoyX);
  Serial.print("Y = "); 
  Serial.println(valJoyY);
  BtnJoy = digitalRead(2);
  Serial.println(BtnJoy);
  Serial.println("pause");
  delay(1000);
}

I used the serial monitor to print the value taken by the joystick. It goes from 0 to 1023, that’s why I had to transform them to be usable with my pump during the machine week. The value taken by the press are 1 or 0 for if it is pressed or not.

Sonar HC SR04

I wanted to try the sonar HC-SR04 to understand how it works. I already seen this sensor working that’s why I found interesting to use it. I took this ultrasonic sensor and search on internet how it works. This website on how to use the sonar HC-SR04 with Arduino were very useful not only for the wiring and the code but also to understand how it works with an ultrasound wave.

I made some notes to understand how the sonar with ultrasound works.

#include <Arduino.h>

// https://create.arduino.cc/projecthub/abdularbi17/ultrasonic-sensor-hc-sr04-with-arduino-tutorial-327ff6

int pinEcho = 2;
int pinTrig = 3;

int time;
int distance;

void setup() {
  pinMode(pinTrig, OUTPUT);
  pinMode(pinEcho, INPUT); 
  Serial.begin(9600); 
}
void loop() {
  // on off the trig pin, the ultrasound is send
  digitalWrite(pinTrig, LOW);
  delayMicroseconds(2);
  digitalWrite(pinTrig, HIGH);
  delayMicroseconds(10);
  digitalWrite(pinTrig, LOW);
  time = pulseIn(pinEcho, HIGH); //receive the value of the ultrasound, valeur de l'aller retour
  distance = time * 0.034 / 2; //calcultate the distance according to the value of sound
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(1000);
}

Here is the value send on the serial monitor.

The sonar is rather not bad because we could set outputs according to the value of the distance. It is rather precise and reactive. I just measured the distance with my hand in front of the sonar and the measures seems correct.

PIR

I tried another sensor, the PIR, an infrared sensor that measure the infrared light emitted by object like human body. Here is the website I use where I found how to use the PIR with Arduino

#include <Arduino.h>

// https://create.arduino.cc/projecthub/electropeak/pir-motion-sensor-how-to-use-pirs-w-arduino-raspberry-pi-18d7fa

int ledPin = 13;
int pirPin = 2;
int pirStat = 0;

void setup() {
 pinMode(ledPin, OUTPUT);     
 pinMode(pirPin, INPUT);     
 Serial.begin(9600);
}
void loop(){
pirStat = digitalRead(pirPin);
Serial.println(pirStat);
if (pirStat == 1) {
  digitalWrite(ledPin, HIGH);
} 
else {
  digitalWrite(ledPin, LOW);
}
delay(500);
} 

The sensor detects something or not, transcribe by a value 0 or 1, as we can see on the terminal.

I found the sensor not really precise, it takes time to come back to 0 after I removed my hand from the front of it, and parasitize. I found it less reactive than the previous one.

Time of flight sensor

The TOF sensor measures the time of the light to come back to the sensor. It sends a photon and measures how many time takes the photon to come back to the captor. I use the sensor TOF VL53L1X.

I found on this website the principle of a TOF sensor and the wiring on a Arduino board.

Here is the principle.

And here is the schematic of the wiring.

I have redone it agian on the arduino board.

I had many troubles to find the right code that can be flashed an the Arduino Uno board. It took me a long time to find a website that explain clearly the principle and the code and how to install the librairy for the sensor.

I came across the pololu librairy with the tutorial on how to install the library on Arduino IDE.

Then I tried to flash the board but it didn’t work. I tried to debug with adding some Serial.printIn among the code.

After being stuck for many hours I went to ask some help to my instructor. He explained me that the way of communication of the sensor is in I2C, seeable on the overview of the sensor here.

So we checked if communication is established in i2c with the adress of the captor (29), and it was ok. After looking for a while, he told to remove

Wire.setClock(400000);

that can cause problems in etablihing the communication with the Arduino Uno.

#include <Arduino.h>
#include <Wire.h>
#include <VL53L1X.h>

// https://makersportal.com/blog/2019/4/10/arduino-vl53l1x-time-of-flight-distance-measurement

VL53L1X sensor;

void setup()
{
  Serial.begin(115200);
  Serial.println("go1");
  Wire.begin();
  //Wire.setClock(400000); // use 400 kHz I2C
  Serial.println("go2");
  sensor.setTimeout(500);
  Serial.println("go3");
  if (!sensor.init())
  {
    Serial.println("Failed to detect and initialize sensor!");
    while (1);
  }
  Serial.println("go3");
  sensor.setDistanceMode(VL53L1X::Long);
  sensor.setMeasurementTimingBudget(15000);
  sensor.startContinuous(15);
  Serial.println("new program");
}

void loop()
{
  Serial.println("go10");
  Serial.println(String(millis())+","+String(sensor.read()));
  delay(500);
} 

It finally worked with Arduino IDE and I want to make it work also with VSCode. I installed the library for VL53L1X for Pololu.

And I flahed the board and here is the value taken in the terminal.

Design of the board with inputs

After testing the different sensor as inputs, I desgined my board with female connectors to be able to plug some sensor, as the sonar. I choose to add 2 4 pins and one 5 pins to be able to connect many inputs. I took as a basis the board SAMD11C I already used during previous weeks and I connect all the pins that are free from the microcontroller.

I draw the schematic and the PCB with KiCad that I already use during previous week

Then with mods I drew the traces.

And the outlines of the board.

I got some problem during the milling of the board. I took a PCB FR that was already stuck on the CNC but it did not stick enough, it was moving up and down, that is why the milling bit digged too much in the PCB and finally broke.

I stuck it again but it was not flat, that why a part of the traces were missing. I launch again the milling of the traces a little bit deeper.

And when it came to the cut of the outlines, the PCB has been took off and the milling bit cut everything.

So I had to mill again my board with a new PCB that I glued myself and every went good.

Then I soldered all the components.

I flashed the board with edbg.

I plugged the sonar but I was not able to make it work.

First I flashed the same code in changing the pin number where the sonar is plugged but I was not able to flash it. It reflashed with the programmer with edbg and reflash again with the code for the sonar and now it is flashed.

But when I opened the serial monitor with Arduino, it print only the value 0 centimeters.

Because the sonar was not working, I checked all the traces with the multimeter, and I checked all the female connectors I soldered with flashing the blink code example in Arduino IDE for each pin as following:

/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Blink
*/

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(5, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(2, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(5, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);                       // wait for a second
  digitalWrite(5, LOW);    // turn the LED off by making the voltage LOW
  delay(500);                       // wait for a second
  digitalWrite(8, HIGH);
  delay(500);
  digitalWrite(8, LOW);
  delay(500);
  digitalWrite(9, HIGH);
  delay(500);
  digitalWrite(9, LOW);
  delay(500);
  digitalWrite(14, HIGH);
  delay(500);
  digitalWrite(14, LOW);
  delay(500);   
  digitalWrite(15, HIGH);
  delay(500);
  digitalWrite(15, LOW);
  delay(500);   
  digitalWrite(4, HIGH);
  delay(500);
  digitalWrite(4, LOW);
  delay(500);
  digitalWrite(2, HIGH);
  delay(500);
  digitalWrite(2, LOW);
  delay(500);   
}

I plugged an LED with a resistor on each output pin and everything was working.

I checked again the Sonar on the Arduino Uno board with putting current at 3.3V and it didn’t work. I checked the picture of the wiring I made before and where it was working and indeed, the sonar was plugged for VCC in 5V. I understood my mistake and since I didn’t wire a 5V connector, I can not use the sonar with my SAMD11C board I designed.

I did not give up and I tried to flash the program for the time of flight sensor in my board, but here again it does not work. The flashing use too much memory region

FLASH' overflowed by 2936 bytes

Here is the copy of the error:

Arduino: 1.8.13 (Windows 10), Board: "Generic D11C14A, Print & String use auto-promoted doubles only, config.h disabled, INTERNAL_USB_CALIBRATED_OSCILLATOR, 732.4Hz (16-bit), 4KB_BOOTLOADER, ONE_UART_ONE_WIRE_NO_SPI, CDC_ONLY"

c:/users/elina/appdata/local/arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/../lib/gcc/arm-none-eabi/4.8.3/../../../../arm-none-eabi/bin/ld.exe: C:\Users\Elina\AppData\Local\Temp\arduino_build_258793/sketch_apr12a.ino.elf section `.text' will not fit in region `FLASH'

c:/users/elina/appdata/local/arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/../lib/gcc/arm-none-eabi/4.8.3/../../../../arm-none-eabi/bin/ld.exe: region `FLASH' overflowed by 2936 bytes

collect2.exe: error: ld returned 1 exit status

Multiple libraries were found for "VL53L1X.h"

 Used: C:\Users\Elina\Documents\Arduino\libraries\VL53L1X-1.2.1

 Not used: C:\Program Files (x86)\Arduino\libraries\VL53L1X-1.2.1

exit status 1

Error compiling for board Generic D11C14A.

I will try to flash via my programmer, to flash my board without bootloader and save 4KB of memory.

Touch sensor

To use the board I made and check if it is working with an input., I took a touch sensor from the Kit Keyes.

I wire it to a female connector, the ground to the ground, the power source in 3.3V and the pin that receive the input is here the pin 4.

Here is the code I wrote to receive the value in digital mode:

int i = 0; // an integer to receive the value

void setup() {
  // put your setup code here, to run once:
  pinMode(4, INPUT); // set the pin 4 as an input
  Serial.begin(9600); // initialize the serial comunication
}

void loop() {
  // put your main code here, to run repeatedly:
  i = digitalRead(4); // read the value of the pin 4, send by the touch sensor
  Serial.println(i); // print the value in the serial monitor
  delay(1000);
}

The values receive by the serial monitor. When I touch the sensor it prints “1”, when I don’t touch it it prints “0”.

All the files