Skip to content

12. Input devices

This week we have been looking at sensors in input. For this I decided to work on the distance sensor that I would use for my final project. This sensor is an ultrasonic sensor. In this documentation I will explain the working principle of an ultrasonic sensor, how to connect it, code it and test it. During this week I also made a new board with several connectors to make this circuit more versatile.

Hero shot

Assignement

Group assignment:

  • Probe an input device(s)’s analog and digital signals
  • Document your work (in a group or individually)

Individual assignment:

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

How do you measure a distance?

To measure distances, you need a distance sensor. There are a large number of distance sensors on the market: infrared (reflective), laser (time-of-travel or angle-based), physical (absolute or incremental optical rules), or ultrasonic.

  • Infrared sensors have the advantage of being cheap, relatively accurate and available almost everywhere. Unfortunately, they are quite complex to implement because of their non-linearities. A complex formula must be applied to obtain a usable measurement. In addition, they are very sensitive to ambient light and the light reflection coefficient of the surface in front of the sensor.

  • Laser distance sensors are extremely accurate, but also extremely expensive. A laser distance sensor (using time-of-travel measurement) can measure more than 30 metres without any problem for some models. So in the end it is a question of budget / use.

  • Physical sensors, most often a duo comprising a graduated ruler and an optical sensor, are both cheap and very accurate. But they are very limited in measurable distance.

  • An ultrasonic distance sensor uses the same principle as a laser sensor, but using sound waves (inaudible) instead of a beam of light. They are much cheaper than a laser sensor, but also much less accurate. However, unlike infrared sensors, ambient light and the opacity of the surface in front of the sensor do not affect the measurement.

Wishing to make a human tracking robot that can move outside and in all conditions, I decided to choose an ultrasonic sensor. This sensor is the most affordable and with a precision that is more than enough for me.

What is an ultrasonic sensor and how does it work?

As the name suggests, the principle of operation is based on the use of ultrasound. These are acoustic waves whose frequency is too high to be heard by humans. An ultrasonic sensor emits short, high-frequency sound pulses at regular intervals. These pulses travel through the air at the speed of sound. When they hit an object, they are reflected back to the sensor as an echo. The sensor then calculates the distance to the target based on the time between the transmission of the signal and the reception of the echo.

Credit : MicroSonic

Virtually all materials that reflect sound can be detected, regardless of their colour. Even transparent materials or thin sheets are no problem for an ultrasonic sensor.

Credit : Sparkfun

For the rest of the week I will be using a HC-SR04 sensor, which works with a 5 volt supply, has a measuring angle of approx. 15° and can measure distances between 2 centimetres and 4 metres with an accuracy of 3mm.

The principle of operation of the sensor is based entirely on the speed of sound. This is how a measurement is made:

  1. The sensor sends a 10µs HIGH pulse to the TRIGGER pin of the sensor.
  2. The sensor then sends a series of 8 ultrasonic pulses at 40KHz (inaudible to humans).
  3. The ultrasound travels through the air until it hits an obstacle and then returns in the other direction to the sensor.
  4. The sensor detects the echo and terminates the measurement. The signal on the ECHO pin of the sensor remains HIGH during steps 3 and 4, which allows the duration of the ultrasonic round trip to be measured and thus the distance to be determined.

The installation

In order to realize my measurements I decided to use an Arduino UNO and a breadboard present at AgriLab. Then I downloaded this code (by changing the pins) that I found on Internet on the Arduino. Here is a picture of the first assembly

The code

This is the final code I used with the SAMD11C. Pins 31 and 14 are not pins of the Arduino Uno.

int trig = 31;
int echo = 14;
long lecture_echo;
long cm;

void setup(){

pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
pinMode(echo, INPUT);
Serial.begin(9600);

}

void loop(){

digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
lecture_echo = pulseIn(echo,HIGH);
cm = lecture_echo /58;
Serial.print("Distance en cm :");
Serial.println(cm);
delay(1000);
}

Declaration of variables and constants

int trig = 31;  

creates a variable of type “int” with the name “trig” which is assigned to PIN 31 and which will be used as a transmitter.

int echo = 14; 

creates a variable of type “int” with the name “echo” which is assigned to PIN 14 and which will be used as a receiver.

long lecture_echo;   

creates a variable of type “long” and the name “lecture_echo” which will be used to store the value that the sensor receives.

long distance_cm;

creates a variable of type “long” and the name of “distance_cm” which represents the distance (which we will calculate in centimetres) which separates the sensor from the object.

The setup

It is important to set Trig to the low state with the following command: digitalWrite(trig, LOW);
In other words, not to operate the transmitter; so that it can be triggered in the Loop.

Trig and Echo are also defined as output and input, by the following commands:

pinMode(trig, OUTPUT);
pinMode(echo, INPUT); 

Don’t forget to indicate that the communication between the board and the Arduino is done at a rate of 9600 bauds with the command :

Serial.begin(9600); 

The Loop :

digitalWrite(trig, HIGH);
delayMicroseconds(10);  
digitalWrite(trig, LOW);

These 3 commands allow to start trig, that is to say the transmitter so that it sends a pulse during 10 microseconds, then stops. As a reminder, one millisecond = 1000 microseconds. So here we don’t use the delay function which is in milliseconds, but the delayMicroseconds function which is in microseconds.

read_echo = pulseIn(echo, HIGH);

Then we store in the variable lecture_echo the value of the pulse that the receiver receives.

This value will then be used to calculate the distance between the sensor and a possible object, which we will store in the variable distance_cm. To do this we will use the following formula:

distance_cm = reading_echo / 58; 

Which represents the pulse emitted by Trig, reflected on the object, then returned to Echo, all divided by 58.

Why 58, you might ask? Because this number represents the time it takes for the impulse to make a round trip to the object, divided by 2 (because what we are interested in is the distance separating the sensor from the object, so the equivalent of the round trip) and multiplied by the speed of propagation of sound in air, that is to say 340m/s since here we are using an impulse in the form of sound waves (ultrasound).

How was this number found?

By applying the following:

Distance = (speed of sound) x (round trip time) ÷ 2

or (340 m/s) x (round trip time) ÷ 2

or (34,000 cm/1,000,000 microseconds) x (time) ÷ 2

or (34/1,000 ) x (time) ÷ 2

i.e. (17/1000) x (time)

so time = 1000 ÷ 17 = 58.82 microseconds = 58 ɥs

Then comes the commands :

Serial.print("Distance in cm: ");
Serial.println(distance_cm);      

Which display the distance in centimetres in the serial monitor, then go to the line.

And we finish with a delay of 1000 milliseconds or 1 second before resuming the Loop.

delay(1000); 

When I realized that everything worked on the Arduino I decided to try it on my board made in week 7. To do this I had to add several wires to connect my board to the sensor. I soldered 4 wires. 2 wires for the power supply (1 5V and 1 GND) and 2 wires for the sensor control (1 wire for the ECHO pin and 1 wire for the TRIGGER pin). Here is the result once soldered.

Test

Once my board was ready to be used I made several tests to check the accuracy of the sensor and especially to begin to understand its advantages and constraints. For that I used a tape measure and an object to return the sound and thus to allow the sensor to measure the distance. Here is the experimentation protocol that I carried out:

  • Position your ultrasound sensor on a flat surface
  • Position a tape measure (meter, decameter or other measuring tool) next to and perpendicular to the sensor. Positioned the 0 set on the edge of the emitters
  • Position your object in front of the sensor and on a desired distance. In my case 10 cm.
  • Adjust your object so that the desired value appears on the screen, then fix your set and your sensor so that it does not get out of place.
  • Move your object and check with the monitor for accuracy.

I first carried out this test on a table with a maximum distance of 1m. Here are some illustrations. I then plotted the measurements in an Excel table and constructed the graphs below

In a second time I carried out another test with a greater distance of measurement. Moreover I positioned the sensor on the edge of the table to limit at most the risks of errors. The sensor does not know where the object is located precisely and can measure the distance between the table and its TRIGGER transmitter.

By taking the results I was able to make a graph showing the accuracy of the sensor

My future robot will evolve the most of the time outside I also made a test outside the building. There can be pressure differences between the inside and outside of a building. This pressure difference can play a role in the propagation of sound in the air. In order to visualise the difference between these two environments, I carried out an outdoor test. The pressure difference can be due to several factors:

  • Difference in temperature which leads to a difference in air pressure
  • Humidity can change the air pressure
  • The wind can have a negative role in the dissipation of sound waves

This test was carried out on April 8, 2021 at 3:15 p.m. on a sunny day, 17°C at the time of the test, with no wind and very high light levels.

Here is the graph resulting from the outdoor test:

Finally I cut with a laser a piece of wood that would be positioned around the sensor. I would like to embed this sensor in my robot and leave only the place of the transmitter/receiver. I wanted to check if the addition of a component around these outputs had a role in the accuracy of the sensor.

Result

Thanks to all these different tests on different materials and environments I could make some conclusions:

  • The sensor is accurate to within +/- 1 cm in all environments and conditions.
  • The addition of a piece of wood around these components does not influence the result and the accuracy of the measurement
  • There is no “material” effect, all materials tested allowed a distance calculation (cardboard, wood, plastic, plexiglass, textile, metal)
  • Very easy to use sensor

Remark: I noticed during my various tests that if one added an angle of inclination on the object then the measurement was distorted. I have to find a solution to this problem for my final project. The user being in movement, the sensor will make inaccuracies during the measurement of distance which could prevent the good functioning of the robot.

Making a board on KiCAD

After having done all my tests with my modified board I decided to make another board with connectors directly on it. First I wanted to design a board with only 4 connectors for only my ultrasonic sensor. However, after thinking about it I decided to make a more versatile board and therefore decided to add several connection pins. For this I was inspired by the board of my instructor Florent. Having several fast connection slots allows me to have a versatile and easy to use board. Moreover I could reuse this board in the following weeks. It looks a bit like a small Arduino but with a SAMD11C inside.

Here is the electrical diagram of my board. This board has 16 connectors. 1 5V connector, 1 3,3V connector, 2 connectors for the GND and the rest for the microcontroller pins.

Here is the schematic of the traces of my board

To know how to add components in KiCAD and make this kind of board I invite you to watch my week 7 where I describe all the steps to design a board.

Soldering

After milling my copper plate I had to solder all the components on it. For that I needed :

  • 1 SAMD11C microcontroller
  • 1 transistor 3,3V 100mA
  • 1 capacitor 1uF
  • 2 resistors 0 ohm
  • 1 resistor 100 ohm
  • 1 LED
  • 4 connectors HDR 4 position 0,1 TIN SMD

Flashing

Not having a 10 pins connector to flash my board directly, I had to find another solution to program it. For that I used a custom cable. For this I used the symbol of the 10 pin connector on KiCAD. On the symbol you can see numbers next to it. On the wire there are also numbers, so I just plugged the right numbers on the right pins.

To flash a board you only need 5 wires :

  • 1 wire for the power (number 1)
  • 1 wire for the ground (number 9)
  • 1 wire for the clock (number 4)
  • 1 wire for the reset (number 10)
  • 1 wire for the DIO (number 2)

All numbers are placed on the connectors of the custom cable. You just have to connect the right numbers together.

For the rest Antonio flashed my board because it was already ready and I just had to plug in my board.

Test with my new board

After creating, editing, milling, soldering, flashing and testing it in real life conditions. I downloaded the code I used with my old board and rewired the wires to the right pin. It works! My board is smaller, more versatile and more efficient than my old one.

Sensor output voltage test :

In order to preserve my microcontroller it is necessary to check its output voltage. The SAMD11C only accepts 3.3V so it is important to check the sensor’s output voltage especially since we are supplying it with 5V. For that I used the oscilloscope. Finally, after checking the output voltage of the sensor is almost 4V. After checking the SAMD11C datasheet, the microcontroller can accept up to 3.63V with a tolerance of 5%, which brings the limit to 3.8115V. It is therefore possible to put the sensor on the SAMD11C, however the microcontroller will wear out prematurely.

How does the sensor measure a distance?

To understand in more detail how an ultrasonic sensor works I used an oscilloscope. With this tool, which allows us to see the signal, we can better understand the working principle of the sensor. As mentioned before, the sensor measures the distance according to the time it takes for the sound to travel back and forth. Let’s check how it works inside the circuit.

As you can see, the further away the object is, the longer the return time is. The microcontroller will therefore calculate the return time according to the speed of sound constant. This is how an ultrasonic sensor works.

All these results were on the echo pad of the sensor but what happens on the trigger pad?

The trigger terminal is the element that sends the sound. When you connect this terminal to the oscilloscope you can see that it is sending out pulses. These pulses correspond to the time when the sonar emits the noise. We can therefore see that it emits a noise at regular time intervals which allows the echo terminal to receive the sound and calculate the time interval to deduce a distance

Mistakes

The first mistake I made this week was to enter the wrong pin in the code which is why I had no results. This little mistake made me lose a little time but also made me think.

The second mistake that was fixed with a lot of luck was on my tracks. When adding the capacitor to my board I forgot to connect it to GND. When I turned on the power I had no current going to my microcontroller. Luckily by rotating the capacitor 90° I was able to solder it to a trace of the GND and add a 0 ohm resistor to transmit the current.

Conclusion

The ultrasonic sensor is a sensor that can be used for my final project. Its accuracy is very well suited for my use. To solve the problem of the angle of inclination of the object in front of the sensor that sends back the sound signal, I could try to place the sensor on a servomotor that would scan from right to left its environment to always follow its user as well as possible. I will try this during the output week.

Group assignment

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.

Update 28/03/2022

During my visit to the FabLab of the University of Sorbonne Paris I had the chance to varnish a board that I had already made for a long time but that I never soldered. For this I used the board I had made and designed during the input device week.

The first step is to clean the electronic board with an rubber and acetone to remove all traces of grease.

Then you have to place a little varnish on the machined plate and spread it with a small spatula.

In order to harden the varnish you have to place it under a UV lamp for 5 minutes so that the varnish reacts well to the light and becomes very hard.

When the varnish is dry, you have to go on the laser to cut a cabarit and then to engrave the location of the welding

Files


Last update: March 28, 2022