Lab 2

Wifi-Controlled LED Stoplight (Arduino)

Online Link

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

 

Objective

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

Materials

References

The following resources were used in this lab:

 

Procedures

  1. Solder the pin attachments through the Wemos D1 mini in order to utilize the breadboard and jump wires and LEDs
  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.
  6. Used the Arduino GPIO pinout to decide which pins to use for this project from https://pinout.xyz/.
  7. In version 1 I wanted to design a system that all it would do was to turn on the lights according to the pins that were on.
    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

    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 green LED to come on using GPIO commands then I started replicating the same process to the other LEDs both server side methods as well as front-end sliders. All server endpoints changed LED state, turned on LED and loaded the page with LED state as querystring.
  2. For the case of blinking LEDs until another command would stop it: I programmed a javascript fetch request to the server, coded an end point to handle it. The algorithm was essentially the following:
    1. Get lightsRelay state.
    2. If still “on” then proceed cycling through the LEDs.
    3. Then repeat step 3.1 above again recursively style not a loop.
    4. If “off” then proceed change lightsRelay state which in turn will break the cycle.
  3. 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 is 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 breadboard, resistors, LEDs 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  , which then after compiling and uploading the code to the Wemos D1 mini you should see something similar to below screenshot:

    1. 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.

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: System Interface – Web Page

 

State: startup

State: greenLed=on

 

 

State: greenLed=off

 

 

Appendix 3: Server Monitor Output

 

 

Appendix 4: Arduino Code

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