14. Interface and Application Programming

Task of Interface and Application Programming

Group assignment:

Link to Group page of Interface and Application Programming

Individual assignment

Summary

The goal of my individual assignment was to design and build an application that interfaces with my custom microcontroller board using a user interface.

1. Hardware Setup

For this assignment, I used the board I designed and printed prevously and then connected an ultrasonic distance sensor.

I configured the pins as follows:

2. Arduino code

I wrote an Arduino sketch to listen for serial commands and control the board accordingly.

        

        #define LED_PIN D0
        #define TRIG    D1
        #define ECHO    D2

        void setup() {
          Serial.begin(115200);
          pinMode(TRIG, OUTPUT);
          pinMode(ECHO, INPUT);
          pinMode(LED_PIN, OUTPUT);
          Serial.println("READY");
        }
        
      

3. Serial Command Interface

In the loop, I used simple string commands sent via Serial to control the board:

        

      void loop() {
      if (Serial.available()) {
        String command = Serial.readStringUntil('\n');
        command.trim();

        if (command == "ON") {
          digitalWrite(LED_PIN, HIGH);
          Serial.println("LED ON");
        } else if (command == "OFF") {
          digitalWrite(LED_PIN, LOW);
          Serial.println("LED OFF");
        } else if (command == "SCAN") {
          digitalWrite(TRIG, LOW);
          delayMicroseconds(2);
          digitalWrite(TRIG, HIGH);
          delayMicroseconds(10);
          digitalWrite(TRIG, LOW);

          long duration = pulseIn(ECHO, HIGH);
          int distance = duration * 0.0343 / 2;

          Serial.print("DISTANCE:");
          Serial.println(distance);
        }
      }
    }
        
      

Combination of all codes

Adruino code
        
      #define LED_PIN D0
      #define TRIG D1
      #define ECHO D2


      void setup() {
        Serial.begin(115200);
        pinMode(TRIG, OUTPUT);
        pinMode(ECHO, INPUT);
        pinMode(LED_PIN, OUTPUT);
        Serial.println("READY");
      }

      void loop() {
        if (Serial.available()) {
          String command = Serial.readStringUntil('\n');
          command.trim();

          if (command == "ON") {
            digitalWrite(LED_PIN, HIGH);
            Serial.println("LED ON");
          } else if (command == "OFF") {
            digitalWrite(LED_PIN, LOW);
            Serial.println("LED OFF");
          } else if (command == "SCAN") {
            digitalWrite(TRIG, LOW);
            delayMicroseconds(2);
            digitalWrite(TRIG, HIGH);
            delayMicroseconds(10);
            digitalWrite(TRIG, LOW);

            long duration = pulseIn(ECHO, HIGH);
            int distance = duration * 0.0343 / 2;

            Serial.print("DISTANCE:");
            Serial.println(distance);
          }
        }
       }
        
      

4. Web Interface

To interact with the board directly, I created a simple web interface using HTML, CSS, and JavaScript. I used the Web Serial API to allow the browser to communicate directly with my board via USB cable

HTML code HTML code HTML code

I structured the page so users can easily connect, send commands, and view responses dynamically.

Note: All files need to be in one folder for proper function.

Web Interface

5. Result

6. Final results

This video shows how I measured the distance at which the object is located. And distance (result) is displayed on web interface.

Download files

Here are the source code files:

HTML, CSS and java code

Arduino code