Skip to main content

Input devices

Missions of this week:
  • Measure something: add a sensor to a microcontroller board that you have designed and read it

In the group assignment, I'd tried to use a joystick as a input component and test it. The reason to choose the joystick is a can generate both digital and analog signal easily, and it is a very suitable electronic component for beginners to learn.
In the individual part, I want to try other sensor that can receive signal without touching.

Ultrasonic sensor

Ultrasonic is a kind of distance sensor. The principle of the ultrasound is shoot out a sound wave, and after it hit a object, it echo back. We can measure travelling time of the sound wave and calculate the distance. This is called echolocation and it’s how bats and dolphins find objects in the dark and underwater.

The version I use is HC-SR04. The datasheet of the UC-SR04 can be checked: https://www.handsontec.com/dataspecs/HC-SR04-Ultrasonic.pdf

Connection

In week8 assignment, I'd design and manufacture the development board by CNC machine, and I'll keep use it as the microcontroller in this week. The core controller is XIOA-ESP32-C3, and here is the datasheet of the board. From the datasheet, I can find out which pin can't be used in the connection.

Based on the instructions, the VCC connects 3.3V / 5V and GND pin connects to the GND. The TRIG pin should connects to a output pin and the ECHO pin should connects to a input pin.

By comparing the my designed development board, I find there are 4 continuous connection pins almost match the requirement, which mean that the sensor can directly connect to the development board without any extra wire.

The only problem is the leftmost pin which should be 3.3V/5V now become a I/O pin(D4). The solution is set the pin to be a output pin and keep outpin a HIGH voltage.

So the adjusted connection will be like this:

Programming

There is tutorial about programming HC-SR04 in Arduino code: https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/
By simplify and adjust the pin number, here is the result:

The programing code
int Echo = D6;
int Trig = D5;
int Vcc = D4;
int duration=0, distance=0;

void setup() {
Serial.begin(9600);
pinMode(Vcc, OUTPUT);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
delay(1000);
digitalWrite(Vcc,HIGH);
}

void loop() {
digitalWrite(Trig,HIGH);
delayMicroseconds(10);
digitalWrite(Trig,LOW);
duration=pulseIn(Echo,HIGH,4000);
//record the time duration between the trig and echo. 4000 is the number of microseconds to wait for the pulse to start
Serial.print("The duration is ");
Serial.print(duration);
Serial.print("µs");
distance=duration*0.0343/2;
//distance=duration time/2 * speed of sound(343 m/s = 0.0343 cm/µs), unit in cm
Serial.print(". The distance is ");
Serial.print(distance);
Serial.print("cm");
Serial.println("");
delay(200);
}

Simulation result: https://wokwi.com/projects/427813842772718593

Testing on the development board:

The result

Summary

Conclusion

In the past, I used to use a Arduino UNO as my mainly development board. Even using the same programming language, differece development board with also affect the programming code:

  • Always remember to check the datasheet. For example in Arduino UNO, both the digital input and analog input will mark clearly(Marked as A0~A5 analog input on the board), but there are no mark in the ESP32 series, so if you don't pay attention and connect to a digital input pin, you will fail to read the analog signal and waste time to debug the code or suspect any component is broken.
  • In difference board, the resolution of the analog input may also different. For example, in Arduino UNO the max analog input is 1023, but in XIAO ESP32C3 is 4095. It will affect the result if you used the analog input directly to calculate something. In this case, adding the map function would be safer.
  • Similar to the point above, the max voltage (the voltage result when the input is set as INPUT_PULLUP) is also different is different version of the board, like Arduino UNO is 5V and ESP32 is 3.3V. Some sensors may not operate well in 3.3V.
  • When connecting a digital button as input, always remember to set the pinMode as INPUT_PULLUP, and a small delay will help to avoid bouncing problem.