Week 06 - Input devices

Task #06

The task for the sixth week is to connect a sensor for measuring physical quantity to a microcontroller and calibrate it.

Ultrasonic Distance Meter

I chose to create a distance meter using these components

📟 Components
Wiring was pretty simple and striaght forward. Both display and ultrasonic sensor need 5 V power from Arduion power pins. The ultrasonic sensor has two more pins for digital input Trigger (>10us pulse initilize sending ultrasonic wave) and for digital output Echo (stays HIGH for the time of flight). The display then uses two analog pins for writing text/pixels.

🔌 Wiring diagram
No image

I used breadboard for connecting all components.

No image

💻 Arduino IDE Code

#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
                
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
                
#define SCREEN_ADDRESS 0x3C // OLED display address
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT); // define display
                
void setup() {
    Serial.begin(9600); // enable serial 
    pinMode(5, OUTPUT);//define ultrasonic TRIGGER pin as output
    pinMode(6, INPUT);//define utrasonic ECHO pin as input
                
    display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS); // start display
    display.clearDisplay(); // Clear display
    display.setTextSize(1); // Normal 1:1 pixel scale
    display.setTextColor(SSD1306_WHITE); // Draw white text
    display.setCursor(0,0); // Start at top-left corner
    // Print text
    display.println(F("Welcome to"));
    display.println(F(""));
    display.println(F(""));
    display.println(F("Ultrasonic"));
    display.setTextSize(2); 
    display.println(F("Distance"));
    display.println(F("Meter"));

    display.display(); // Show on display
    delay(2000); // Wait 2 s
}
                
void loop() {
    display.clearDisplay();
    digitalWrite(5, LOW); // ensure pin is LOW first
    delayMicroseconds(10);
    digitalWrite(5, HIGH); // generate pulse HIGH for Trigger pin
    delayMicroseconds(10); // for 10 us
    digitalWrite(5, LOW);

    // time of flight in us
    long pulse_lenght_us = pulseIn(6, HIGH); // measure the time of pulse at Echo pin

    // ((pulse_lenght_us / 1 000 000) [s] / 2) * (340 000 [mms/s])
    long distance_mm = (pulse_lenght_us * 17)/ 100; // distance in mm 

    if (distance_mm > 4000){ // if too far, show text "Too Far"
    display.setTextSize(2);
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0,0);
    display.println(F("Measured"));
    display.println(F("Distance:"));
    display.println(F(""));
    display.println(F("Too Far"));
    display.display();
    }
    else if(distance_mm > 20){ // show measured distance
    String text_mm = " mm";
    display.setTextSize(2);
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0,0);
    display.println(F("Measured"));
    display.println(F("Distance:"));
    display.println(F(""));
    display.println(distance_mm + text_mm);
    display.display();
    }
    else{
    display.setTextSize(2); // if too close
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0,0);
    display.println(F("Measured"));
    display.println(F("Distance:"));
    display.println(F(""));
    display.println(F("Too Close"));
    display.display();
    };

    delay(500); // Wait before repeating the loop
}

I coded it using Arduino IDE and loaded the program using microUSB cable. I was also inspired by Matej Mihulka.

🎞️ Demo video

📈 Transfer characteristic
To get the desired distance, you need to the formula The velocity of sound in the air is approximate 340 m/s. However, we need to divide the time by 2, because it counts for both traveled distance, there and back.

According to the graph, the measurements are pretty accurate for static box as reference point. With more distance, the measured distance is a bit shorter than the real one.

No image