Input Devices

The assignment was about taking a sensor as an input and read out its values. I chose an distance-sensor and connected it to my board.

In the beginning I declared the trigger and echo pin, which were used to operate this sensor. Also I added duration and distance as variables.

The setup was used to declare the pinmodes of the trigger and echo to output and input. Also I printed "Program started" again so its easier to verify if everything worked correctly.

The loop was a little more complicated. First you have to send no signal with the trigger, wait a short time and then send out a signal for 10 milliseconds. After that set it on low again and then you can write the duration into a variable with taking the pulse in.

Then you just have to convert this duration into a distance by dividing with 2 because you only want one way and then multiplying with the speed of sound. The reason for this is because the sensor uses an ultrasonic beam and checks when it comes back.

The printing out only triggers when the distance values are greater than zero and lower than 5 meters. Here you can see the serial monitor. The 2.3 meters are the height of the room. Then there was a hand held over it and later it was pointed to the wall to see if everything was working.