Skip to content

6. Embedded programming

Group assignment:

  • Browse through the data sheet for your microcontroller

  • Compare the performance and development workflows for other architectures click here

Individual assignments:

  • Write a program for a microcontroller development board that you made, to interact (with local input &/or output devices) and communicate (with remote wired or wireless devices)

  • Extra credit: use different languages &/or development environmentsextra

  • Credit: connect external components to the board

DATA SHEET

Today we’ll be using a ESP32-WROOM-32 MODULE.

I. PRESENTATION

The ESP32-WROOM-32 is a powerful, generic Wi-Fi+BT+BLE MCU module that targets a wide variety of applications, from low-power sensor networks to the most demanding tasks, such as voice encoding, music streaming and MP3 file decoding. The heart of this module is the ESP32-D0WDQ6 chip. The integrated chip is designed to be scalable and adaptive. There are two CPU cores that can be individually controlled, and the CPU clock frequency is adjustable from 80 MHz to 240 MHz. from 80 MHz to 240 MHz. Users can also switch off the CPU and use the low-power coprocessor to continuously monitor peripherals for changes or threshold crossings. The ESP32 integrates a rich set of peripherals, capacitive touch sensors, Hall sensors, SD card interface, Ethernet, high-speed SPI, UART, I²S and I²C. Data sheet

An OLED display is a type of screen that uses organic light-emitting diodes to display images. It is often used with microcontrollers such as Arduino to display information.

The DHT11 is a very popular humidity and temperature sensor among Arduino enthusiasts. It’s easy to use and affordable. This sensor can be connected to an Arduino board to measure both relative humidity and ambient temperature. Using a specific library, the data collected by the DHT11 sensor can be easily displayed and analyzed. This makes it a common choice for projects linked to environmental monitoring or home automation.

II. PIN

PROGRAMMING

I. programming on arduino IDE

Preparation of the arduino IDE

To begin with, we need to download the arduino software.

Now that the software has been downloaded, it needs to be prepared for easier use.

A. Librairy

Navigate to File > Preferences, and fill “Additional Boards Manager URLs” with the url below :

Add the ESP32 board to your Arduino IDE. Copy the library link into this field

Navigate to Tools > Board > Boards Manager…, type the keyword “esp32” in the search box, select the latest version of esp32, and install it.

We can now program😍​😍​😍​

Navigate to Tools > Board > ESP32 Arduino and select “ESP32 Dev Module”. The list of board is a little long and you need to roll to the buttom to reach it.

Navigate to Tools > Port and select the serial port name of the connected ESP32 Dev Module. This is likely to be COM7 or higher (COM3 and COM7 are usually reserved for hardware serial ports).

B. CODE

We found the code on the randomnerdtutorials site and adapted it to our needs.

Libraries Included

  • SPI.h: Used for communication via the Serial Peripheral Interface.
  • WiFi.h: Provides Wi-Fi connectivity functions.
  • Wire.h: Facilitates I2C communication.
  • Adafruit_GFX.h: Graphics library for rendering shapes and text on displays.
  • Adafruit_SSD1306.h: Specific library for controlling SSD1306 OLED displays.
  • Adafruit_Sensor.h: Base library for sensor management.
  • DHT.h: Library for reading data from DHT temperature and humidity sensors.
#include <SPI.h>
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>

Display Configuration

  • SCREEN_WIDTH and SCREEN_HEIGHT define the dimensions of the OLED display.
  • display is an object of the Adafruit_SSD1306 class, configured for I2C communication.
#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

DHT Sensor Configuration

  • DHTPIN specifies the digital pin connected to the DHT sensor.
  • DHTTYPE defines the type of DHT sensor used (DHT11 in this case).
  • dht is an object of the DHT class, initialized with the specified pin and sensor type.
#define DHTPIN 14       // Digital pin connected to the DHT sensor

// Uncomment the type of sensor in use:
#define DHTTYPE    DHT11     // DHT 11
//#define DHTTYPE    DHT22     // DHT 22 (AM2302)
//#define DHTTYPE    DHT21     // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

Setup Function

  • Serial.begin(115200): Initializes serial communication at 115200 baud.
  • dht.begin(): Initializes the DHT sensor.
  • display.begin(): Initializes the OLED display. If initialization fails, an error message is printed, and the program enters an infinite loop.
  • delay(2000): Waits for 2 seconds.
  • display.clearDisplay(): Clears the OLED display.
  • display.setTextColor(WHITE): Sets the text color to white.
void setup() {
  Serial.begin(115200);
  dht.begin();
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();
  display.setTextColor(WHITE);
}

Loop Function

  • delay(5000): Pauses the loop for 5 seconds.
  • dht.readTemperature() and dht.readHumidity(): Read the temperature and humidity values from the DHT sensor.
  • isnan(h) || isnan(t): Checks if the readings are valid. If not, an error message is printed.
  • display.clearDisplay(): Clears the OLED display.
  • display.setTextSize(): Sets the text size for the display.
void loop() {
  delay(5000);
  // read temperature and humidity
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
  }
  // clear display
  display.clearDisplay();
  // display temperature
  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("Temperature: ");
  display.setTextSize(2);
  display.setCursor(0,10);
  display.print(t);
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);
  display.write(167);
  display.setTextSize(2);
  display.print("C");
  // display humidity
  display.setTextSize(1);
  display.setCursor(0, 35);
  display.print("Humidity: ");
  display.setTextSize(2);
  display.setCursor(0, 45);
  display.print(h);
  display.print(" %");
  display.display();
}

RESULTAT

II. programming on MicroPython + Thonny

MicroPython is an interpreter for the Python 3 programming language, it includes a small subset of the standard Python library and is optimized to run on microcontrollers. For more information click here

A. Installation

The installation of Thonny is very simple go to this site and download the version corresponding to your operating system as shown below (Windows in our case)

Once you have downloaded the Thonny installation file, open it and you just have to click on next to install it.

Launch the Thonny, click on “Tools → Options” to open the settings. as shown below.

Chose the Interpreter interface and select the device as MicroPython(MicroPython ESP32) and the port as Try to detect prot automatically.

Press and hold the BOOT button and connect the card to your PC via the cable and click on Install or Update MicroPython. A window will open and click on Install.

Once the installation is finished, click on “Close”. If everything is OK, you should have the following window.

B. CODE

   
import dht
from machine import Pin, SoftI2C
import time
import ssd1306

i2c = SoftI2C(scl=Pin(22), sda=Pin(21))

oled = ssd1306.SSD1306_I2C(128,64, i2c)

sensor_data = dht.DHT11(Pin(14))

def call_dht():
    sensor_data.measure()
    global temp
    global hum
    temp = sensor_data.temperature()
    hum = sensor_data.humidity()
    print('Temperature -', temp, 'Humidity', hum)

while True:
    call_dht()
    oled.text('Temperature - ',0,0)
    oled.text(str(temp),110,0)
    oled.text('humidity',0,10)
    oled.text(str(hum),110,10)
    oled.show()
    time.sleep(1)
   

RESULTAT

FILES


Last update: July 9, 2024