Week 9: Radio, Wi-Fi, Bluetooth (IoT)

This week, we focused on radio, Wi-Fi, and Bluetooth (IoT).

We were tasked with demonstrating communication between a microcontroller and another device. I completed the assignment by following two tutorials, one on the PHYSCI 70 website by Rob Hart and the other on randomnerdtutorials.com by Rui Santos. Documentation for both projects is provided below.


Project 1: Communicating with UART Serial

I followed the tutorial linked here to facilitate communication between the Adafruit Metro microcontroller and an ESP32-based Feather board. The goal was to have a potentiometer connected to the microcontroller, which would communicate with the ESP32 board to control the position of a servo motor connected to the latter.

On my first attempt, I replicated the wiring setup in the tutorial and tried to upload the code for both the Metro and ESP32 board provided in the tutorial. The wiring setup and code from the tutorial are shown below.

Wiring setup:

Metro microcontroller code:


        //Code to transmit
        //Microcontroller has a potentiometer input:  wiper pin attached to analog in.
        //Sends a signal to receiver through the USART transmit pin (TX)  (or TX1 for some boards)

        //Can receive  a signal throught the USART receive pin (RX), but this is not
        // implemented in this simplest sketch.

        //The wired-together devices must share a common ground!

        const int pot_pin = A0;

        void setup() {
          Serial.begin(0);        //for the USB serial devices, this baud rate is meaningless - can be "0"
          Serial1.begin(9600);      //this is the USART SERIAL.  On SAM boards, like Metro M0, ItsyBitsy and D11, this is called "Serial1". Baud rate is important on this one!           
        }

        void loop() {
        int pot_value = analogRead(pot_pin);
        Serial.println(pot_value);          //This is for diagnostic purposes
        Serial1.write(pot_value/4);     //This one is data sent to the other board.  divide by four to make the range 0-255.
        delay(10);
        }
      

ESP32 board code:


        //Code to move a servomotor in response to a change in input pin.
        //Use as a simple example of receiving for a wired network 3/30/20.  Rob Hart.
        //modified to use with ESP32 Huzzah board July 2020. R.Hart

        #include 

        Servo myservo;  // create servo object to control a servo

        void setup() {
          myservo.attach(21);  // attaches the servo on pin 9 to the servo object
          Serial.begin(9600);        //for the USB serial devices, this baud rate is meaningless - can be "0"
          Serial1.begin(9600);      //this is the USART SERIAL.  On SAM boards, like ItsyBitsy and D11, this is called "Serial1"
          //Baud rate is important on this one!
        }

        void loop() {
          while (!Serial1.available()) {      //wait until serial from UART is available.
          }

          byte rec_byte =  Serial1.read();    //Here is where the incoming byte is read.
          //Serial.println(rec_byte);       //for diagnosing using USB serial
          int degree = rec_byte*180/255;      //conversion between one byte and 180 degrees.
          Serial.println(degree);           //for diagnosing using USB serial
          myservo.write(degree);
        }
      

The code for the microcontroller and the ESP32 board would not upload and the program instead returned error messages for both. First, I focused on the microcontroller code. I quickly realized that the tutorial uses an Adafruit Metro Express microcontroller, which has two serial ports versus the microcontroller I was using, which only has one. Due to the fact that there was no "Serial1" on my board, the code would not upload without an error. So, I deleted the "Serial1.begin(9600);" line under void setup(){} and replaced the "Serial1.write(pot_value/4);" line under void loop(){} with "Serial.write(pot_value/4);". I also commented out the "Serial.println(pot_value);" line in the code. Having made these changes, I also changed the wiring so that instead of one signal wire connecting the TX1 pin on the microcontroller to the RX/16 pin on the ESP32 board, there were two wires (one connecting the microcontroller TX1 pin to the RX/16 pin on the ESP32 board and the other connecting the microcontroller RX0 pin to the TX/17 pin on the ESP32 board).

With respect to the code for the ESP32 board, I realized that the issue was the use of the ESP32Servo.h library. I downloaded the ESP32 Arduino Servo Library linked here and replaced the "#include <ESP32Servo.h>" line in the original code with "#include <Servo.h>". This fixed the uploading issue, and with that I was ready to test the device. The modified wiring setup and code are shown below.

Modified wiring setup:

Modified Metro microcontroller code:


        //Code to transmit
        //Microcontroller has a potentiometer input:  wiper pin attached to analog in.
        //Sends a signal to receiver through the USART transmit pin (TX)  (or TX1 for some boards)

        //Can receive  a signal throught the USART receive pin (RX), but this is not
        // implemented in this simplest sketch.

        //The wired-together devices must share a common ground!

        const int pot_pin = A0;

        void setup() {
          Serial.begin(9600);   
        }

        void loop() {
        int pot_value = analogRead(pot_pin);
        //Serial.println(pot_value);          //This is for diagnostic purposes
        Serial.write(pot_value/4);     //This one is data sent to the other board.  divide by four to make the range 0-255.
        delay(10);
        }
      

Modified ESP32 board code:


        //Code to move a servomotor in response to a change in input pin.
        //Use as a simple example of receiving for a wired network 3/30/20.  Rob Hart.
        //modified to use with ESP32 Huzzah board July 2020. R.Hart

        #include 

        Servo myservo;  // create servo object to control a servo

        void setup() {
          myservo.attach(21);  // attaches the servo on pin 9 to the servo object
          Serial.begin(9600);        //for the USB serial devices, this baud rate is meaningless - can be "0"
          Serial1.begin(9600);      //this is the USART SERIAL.  On SAM boards, like ItsyBitsy and D11, this is called "Serial1"
          //Baud rate is important on this one!
        }

        void loop() {
          while (!Serial1.available()) {      //wait until serial from UART is available.
          }

          byte rec_byte =  Serial1.read();    //Here is where the incoming byte is read.
          //Serial.println(rec_byte);       //for diagnosing using USB serial
          int degree = rec_byte*180/255;      //conversion between one byte and 180 degrees.
          Serial.println(degree);           //for diagnosing using USB serial
          myservo.write(degree);
        }
      

Video:

As you can see in the pictures and video above, I powered both the Metro microcontroller and ESP32 separately using USB cables connected to my laptop. I also wanted to see if the microcontroller could be used to power the ESP32 board. I first tried connecting the 3.3V pin on the microcontroller to the 3V pin on the ESP32 board without success. After looking at the pinout diagram of the ESP32 board, I noticed that there was a USB pin. I promptly connected the 5V pin from the microcontroller to the USB pin on the ESP32 board, and it powered on.

Wiring setup:

Video:


Project 2: ESP32 Servo Motor Web Server with Arduino IDE

For my second project, I followed the tutorial linked here to facilitate communication between the ESP32 board and a servo motor through a web server. The goal was to have a slider on the web server to control the position of the servo motor (between 0º and 180º) using a pulse width modulation (PWM) signal.

I attached the servo motor to the 13/A12 pin on the ESP32 board and uploaded the code from the tutorial (linked here) to it after substituting the wifi name and password for "REPLACE_WITH_YOUR_SSID" and "REPLACE_WITH_YOUR_PASSWORD". I then copied the code from the tutorial (linked here) into a new Sublime Text file and saved it as an HTML file. You can also use Notepad for this step if you want! For a detailed breakdown of the code, please refer to the tutorial.

After uploading the code onto the ESP32 board, I opened the serial monitor at a baud rate of 115200 and took note of the IP address it returned (192.168.0.180). Here is a screenshot of the Arduino IDE and serial monitor with the IP address boxed in red:

I entered the IP address into my browser and the webpage I created popped up with the slider. Here is a picture of the setup and a video of the project in action:

Wiring setup:

Video:

Both projects were really neat! Now I know how to communicate with UART serial and how to control a servo through a microcontroller via input from a web server.

And that is all for this week. Thank you!

Powered by w3.css

Mohammed Mutaher 2022