Skip to content

11. Input devices

This week’s aim was to design a board witth MCU in it and add a sensor and read the data from it.

Ultrasonic Sensor HC-SR04

HC SR04 source:https://www.makerlab-electronics.com/product/ultrasonic-sensor-hc-sr04/

One is emitter and the other is receiver

Sensor is something that sense the changes in physical paramters like humidity, temparature,pressure,motion,light etc..and convert them to electrical signals. These electrical signals are send to a processor/MCU to interpret these changes and measure them.

Principle

Ultrasonic sound waves are those with frequency range more than 20 kilohertz. They are inaudible to us humans. They are used by animals like Whales,Bats etc. for their motion. In SONAR in ships and submarines they are extensively used.

In our sensor it emits an ultrasound at 40 000 Hz which travels through the air and if there is an object or obstacle on its path It will bounce back to the module. Considering the travel time and the speed of the sound you can calculate the distance.
HC SR04 working
Source:http://learn.parallax.com/tutorials/robot/activitybot/propeller-c-programming-activitybot/navigate-ultrasound/build-and-test

How to use the sensor to get the distance. We have to ensure that the object is aligned perpendicular to the sensor.

Inorder to generate the ultrasound, the microcontroller is made to generate a 10us pulse and send it to the trigger pin of the sensor module. The sensor module will inturn send a 8 cycle sonic burst at the speed of sound to the object. This in turn will be reflected back to the Echo pin and then to the MCU. The MCU has an internal timer arrangement where it measures the time period for which the Echopin’s signal is high. This time period is directly related to the distance travelled by the sonic wave to reach the object from the sensor and return to the sensor and the speed of the wave in air which is 340m/s or 0.034cm/us. HC SR04
Source : https://electrosome.com/hc-sr04-ultrasonic-sensor-pic/

Making the PCB

First the schematic circuit was made using Eagle software Circuit Schematic Board Schematic Circuit Schematic Board Circuit Milling Board was milled using Roland MX 20 with V bit. The cutting of board was not done. Milling The trace at the corner was not milled off properly. I used a knife to manually to make the trace and was connected by applying solder along the top. After soldering the components were checked fof the continuity of the traces.

Eagle Files here

Programming

I decided to go ahead with Arduino IDE as I found in week 9 that it is pretty cool.Code from this source was referred for programming.

To enable communication, the software serial library has to be added to enable USART communication.
##include <SoftwareSerial.h>
The USART stands for universal synchronous and asynchronous receiver and transmitter. It is a serial communication of two protocols. This protocol is used for transmitting and receiving the data bit by bit with respect to clock pulses on a single wire. The Attiny 44 microcontroller has two pins: TXD and RXD, which are specially used for transmitting and receiving the data serially. (Source: https://www.elprocus.com/avr-microcontroller-serial-data-communication/)

PS: I thank Suhail P for helping with this after an hour of debugging.

My first step was to check whether the serial communication is working or not.A test programme was used to check the serial communication. Before enabling serial communication necessary access priveleges are given by typing sudo chmod 777 /dev/ttyUSB in the terminal. serial communication test

It returned error to show that the serial communication is not happening ! Something is wrong.

Finally the program from the Week 7 the echo program was flashed on the MCU to check whether it’s communicating using cutecom. This showed that nothing is wrong with the Computer’s software.

serial communication test The result was that the break was kept coming which meant that some sort of partial communication is happening.

Again I checked with multimeter to check whether the connections are alright though i had checked it before hand. I just found that at the base of RX pin near MCU the solder was partial. I soldered it to fix it. serial communication test Finally it worked on the serial monitor.

         Distance calculator
  // This programme is used to calculate distance using Ultra sonic sensor HC-SR04
  // Manu Mohan S,FabAcademy 2019, FABLAB Trivandrum
  // Trigger pin of the sensor connected to PA2
  // Echo pin connected to PA3

  #include <SoftwareSerial.h>

  // defined the pin numbers
  const int trigPin = PA2;
  const int echoPin = PA3;

  // define the variables

  long duration;
  int distance;

  SoftwareSerial ss(1,0);

  void setup()
  {

  pinMode (trigPin,OUTPUT);// sets trigPin as output
  pinMode (echoPin,INPUT); // sets echoPin as input
  ss.begin(9600);//start serial communication at 9600 baud rate

  }

  void loop()
  {
  // clears the trigger pin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Sets the trigPin for High state for 10microseconds to generate the pulse

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

  // reads the echoPin,to get the ultrasonic wave travel time
  duration = pulseIn(echoPin, HIGH);

  // Calculating the distance
  distance= duration*0.034/2;

  //prints the distance on serial monitor
  ss.print ("Distance: ");
  ss.println (distance);

  }

I could see that the distance was changing rapidly on the serial monitor and it worked ! serial communication test

Video here