Lab 3

Machine to Machine Communication Vehicle Sensor

Online Link

This lab is available as part of my online portfolio at: https://www.lehi.dev

 

Objective

The purpose of this lab is to learn how to create a machine to machine communication, vehicle sensor, wifi-controlled stoplight using Arduino. These principles include:

Materials

References

The following resources were used in this lab:

 

Procedures

  1. Solder the headers on 2 Wemos D1 mini in order to utilize the breadboard and jump wires and LEDs and Ultrasonic Sensor HC-SR04.
  2. Download and install Arduino IDE from https://www.arduino.cc/en/Main/Software
  3. Install CH341 version 1.4 from the following website: https://kig.re/2014/12/31/how-to-use-arduino-nano-mini-pro-with-CH340G-on-mac-osx-yosemite.html
  4. Add Wemos D1 library to the Arduino IDE:
    1. Arduino > Preferences > (in addition Boards Manager URLs paste the following: https://kig.re/2014/12/31/how-to-use-arduino-nano-mini-pro-with-CH340G-on-mac-osx-yosemite.html)
    2. Tools > Board > Boards Manager > (search for “esp8266”) then click install.
    3. Tools > Board > “LOLIN(WEMOS) D1 R2 & mini”
    4. Then selected the connected arduino by going to:
      1. Tools > Port > /dev/cu.wchusbserial14510
  5. Used the Arduino IDE to develop the code for the stoplight and Ultrasonic Sensor HC-SR04.
  6. Used the Arduino GPIO pinout to decide which pins to use for this project from https://wiki.wemos.cc/products:d1:d1_mini#documentation.
  7. In version 1 I wanted to design a system that all it would do was to measure the distance in centimeters of an object and the Ultrasonic Sensor HC-SR04 and print out the distance using Serial Monitor.
  8. I then reused the existing lab 2 source code and breadboard with Wemos D1 mini, resistors and led lights and ran a web server to accept commands to turn on and off led lights.
    1. I Chose the following pins to be on:
      1. Pin D6: Green color
      2. Pin D8: Yellow color
      3. Pin D1: Red color
      4. Pin G: Ground
lab3

    1. I had one Ground pin from GPIO to breadboard negative column, then connected 220 Ω resistors to LEDs negative end point.
  1. In version 2, once I got the Ultrasonic Sensor HC-SR04 working and printing out the distance of an object in comparison to the Ultrasonic Sensor HC-SR04. I then started the process to sending a GET request protocol to the existing arduino stoplight.

  1. The logic set up was the following in pseudo code:
    1. if distance > farAway then turn on green led light    
    2. if distance > caution then turn on yellow led light
    3. if distance > perfectDistance then turn on red led light
    4. if distance > tooFar then blink the turning on yellow led light
    5. Keep track which led is currently on, it will then be used to turn off certain lights that should be off as the state changes.
    6. Turn on the led that should be on
  2. Models:
    1. Functionality: State machine as shown on the diagram below, this is a simplified form of the state machine as in reality I have created many endpoints to change the state machine for each led, but the basic concept is described below for each endpoint it reacts as the following diagram:

    1. System Flow: See Appendix 1.
    2. Component and Schematic Diagram: The stoplight system and the Ultrasonic Sensor HC-SR04 are represented below by the following diagrams (Component and Schematic Diagram respectively)

 

 

 

 

Diagrams were facilitated by open source programs such as: Google drive plugin draw.io Diagrams and Fritzing.

 

  1. Program the code in the Arduino IDE
    1. Code the Arduino GPIO pins interaction with breadboards, resistors, LEDs, Ultrasonic Sensor HC-SR04 accordingly to match the previous component/schematic diagram.
    2. Test/Debug constantly so the final product would be bug free. You can debug by first making sure the baud rate is set up in setup method. Then click on serial monitor icon  .
    3. Use commands such as Serial.println(<variableToBeEvaluated>) to print out debug messages, so you can make sure what you think the code is doing is actually happening.
    4. I had some trouble uploading code to arduino and opening up the Serial monitor for debugging purposes. I had to resort if wanting to debug, to unplug the echo  jump wire and then open up the Serial monitor for debugging only on the beginning of the code run.

Thought Questions

 

Certification of Work

I certify that the solution presented in this lab represents my own work. In the case where I have borrowed code or ideas from another person, I have provided a link to the author’s work in the references, and included a citation in the comments of my code.

 

— Lehi Alcantara

Appendix

Appendix 1: System Flowchart

Appendix 2: Stoplight Server Monitor Output

Appendix 3: Ultrasonic Sensor HC-SR04 Monitor Output

Appendix 4: Arduino Code

Sensor.ino:

(Available at https://github.com/ylehilds/iot-sensor/blob/master/sensor/sensor.ino)

arduinoStoplight.ino:

(Available at https://github.com/ylehilds/arduinoStoplight)