Skip to content

12. Input Device

Group assignment:

  • Probe an input device’s analog levels and digital signals

Individual assignment:

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

Have you answered these questions?

  • Linked to the group assignment page
  • Documented what you learned from interfacing an input device(s) to microcontroller and how the physical property relates to the measured results
  • Documented your design and fabrication process or linked to previous examples.
  • Explained the programming process/es you used
  • Explained problems and how you fixed them
  • Included original design files and source code
  • Included a ‘hero shot/video’ of your board

What I’ve done this week

Group assignment:

Individual assignment:

  • Read datasheet HC-SR04 (ultrasonic distance sensor)

  • Design board

  • Program

  • Assemble

Read detasheet HC-SR04 (ultrasonic distance sensor)

First, I read the HCSR04 datasheet

Wire connecting direct as following:

  • 5V Supply
  • Trigger Pulse Input
  • Echo Pulse Output
  • 0V Ground

Electric Parameter

Electric Parameter parameter
Working Voltage DC 5 V
Working Current 15mA
Working Frequency 40Hz
Max Range 4m
Min Range 2cm
MeasuringAngle 15 degree
Trigger Input Signal 10uS TTL pulse
Echo Output Signal Input TTL lever signal and the range in proportion
Dimension 45 x 20 x 15mm

Design board

Next, designed the board in Kicad.

I used ATTiny3216 for the microcontroller board

ATTiny3216, ATTiny1614 have servo.h library, so I used ATTiny3216, which the lab had plenty of in stock.

For ATtiny3216 to control the ultrasonic sensor and servo motor, connected the trigger pin of the ultrasonic sensor to PA4 (pin 0) and the echo pin to PA5 (pin 1).

Also connected the servo signal pin to PB0 (pin 9).

Wired as follows

The electronic parts are as follows

The board is as follows

Program

Referred to the following article

Arduino - Ultrasonic Sensor

First, show the flow of the ultrasonic sensor in the program

By sending a 10ms pulse from the ATTiny3216 to the Triger pin, ultrasonic waves can be emitted from the ultrasonic sensor.

The ultrasonic waves hit the object, bounce back, and are received by the Echo pin.

Definition of variables to be used this time

The travel time of the ultrasonic wave (ms):

travel_time = pulse_duration

The speed of the ultrasonic wave:

speed = SPEED_OF_SOUND = 340 [m/s] = 0.034 [cm/ms]

The travel distance of the ultrasonic wave (cm):

travel_distance = speed x travel_time = 0.034 x pulse_duration

The distance between sensor and obstacle (cm):

distance = travel_distance / 2 = 0.034 x pulse_duration / 2 = 0.017 x pulse_duration
#include <Servo.h>

const int TRIG_PIN  = 0; 
const int ECHO_PIN  = 1;  
const int SERVO_PIN = 4; 

const int DISTANCE_THRESHOLD = 20;

Servo servo;

float duration_us, distance_cm;

void setup() {
  Serial.begin (9600);
  pinMode(TRIG_PIN, OUTPUT); 
  pinMode(ECHO_PIN, INPUT); 
  servo.attach(SERVO_PIN);  
  servo.write(0);
}

void loop() {

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

  duration_us = pulseIn(ECHO_PIN, HIGH);

  distance_cm = 0.017 * duration_us;

  if (distance_cm < DISTANCE_THRESHOLD){

    servo.write(180); 
  } else {
    servo.write(0); 
  }

  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);
}

Explanation of Codes

Define the numbers of the trigger pin and echo pin of the ultrasonic sensor and the signal pin of the servo motor

const int TRIG_PIN  = 0; 
const int ECHO_PIN  = 1;  
const int SERVO_PIN = 4; 

Define distance thresholds

const int DISTANCE_THRESHOLD = 20;

Declares a Servo object

Servo servo;

Define variables with the float type.

float duration_us, distance_cm;

Codes of void setup()

void setup() {
  Serial.begin (9600);
  pinMode(TRIG_PIN, OUTPUT); 
  pinMode(ECHO_PIN, INPUT); 
  servo.attach(SERVO_PIN);  
  servo.write(0);
}

Prepare serial communication (9600 bps)

Serial.begin (9600);

Set the trigger pin as output and the echo pin as input

pinMode(TRIG_PIN, OUTPUT); 
pinMode(ECHO_PIN, INPUT); 

Assign servo variables to pin.

servo.attach(SERVO_PIN);  

Set the angle of the servo motor to 0 degrees

servo.write(0);

Codes of void loop()

void loop() {

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

  duration_us = pulseIn(ECHO_PIN, HIGH);

  distance_cm = 0.017 * duration_us;

  if (distance_cm < DISTANCE_THRESHOLD){

    servo.write(180); 
  } else {
    servo.write(0); 
  }

  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);
}

Switches from HIGH to LOW at intervals of 10μs on the trigger pin

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

Reads a pulse (either HIGH or LOW) on a pin.

pulseIn(pin, value, timeout)

pin: the number of the pin on which you want to read the pulse. (int)

value: type of pulse to read: either HIGH or LOW. (int)

timeout (optional): the number of microseconds to wait for the pulse to be completed: the function returns 0 if no complete pulse was received within the timeout. Default is one second (unsigned long).

duration_us = pulseIn(ECHO_PIN, HIGH);

Calculate the distance

distance_cm = 0.017 * duration_us;

If distance_cm is shorter than the threshold value (20cm), it rotate 180 degrees.

if (distance_cm < DISTANCE_THRESHOLD){

  servo.write(180); 
} else {
  servo.write(0); 
}

Display distance_cm on the serial monitor every 0.5 seconds

  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);

I have created the following electronic circuit this time

Write the program and check serial communication

Assemble

Attached it in the trash box of the Final project that is currently in production (2022/4/23).

What I learned

  • The use of the ultrasonic sensor as an input allowed to interact with the machine and expand the possibilities.

Appendix


Last update: May 2, 2022