6. Embedded Programming

What is embedded system?

  • Embedded systems are computers with set software and hardware components, commonly found in devices like phones, cars, and even refrigerators. Despite their small size and limited resources, embedded systems can have substantial control over their host devices. They are designed for specific functions, making them efficient for various industries, including consumer electronics, healthcare, and transportation. Embedded systems are expected to be reliable, environmentally friendly, and cost-effective, providing several advantages over regular computers.

  • Microcontrollers and microprocessors

  • Microprocessors and microcontrollers are integral components in many electronic devices, serving different but complementary roles. A microprocessor is essentially the brain of a computer or a similar device, handling tasks related to computation and data manipulation. It is typically used in devices where general-purpose computing is required, such as in desktop and laptop computers. On the other hand, a microcontroller is a compact integrated circuit that contains a processor core, memory, and input/output peripherals all on a single chip. Microcontrollers are designed for specific tasks and are commonly used in embedded systems where automation is required, such as in washing machines, air conditioners, and automotive systems. While both microprocessors and microcontrollers are crucial for automation in electronic devices, they differ in their complexity and intended use. Microprocessors are more suited for tasks that require general computing power, while microcontrollers excel in applications that require real-time processing and control.

  • What is microprocessor?

  • A computer, as its name suggests, is a device for processing data in accordance with predetermined sets of instructions. The term "microprocessor" is used to describe this tiny electronic component. A microprocessor is a single-chip computer that can do many processing tasks. The central nervous system of a computer or other electronic device is the microprocessor. Because it's sole function is to follow directions given to it and process data in order to provide results. The Central Processing Unit (CPU) of a microcomputer is a microprocessor. Typically, a microprocessor will have an arithmetic logic unit (ALU) and a control unit (CU). Examples of microprocessors include the Intel 8085 and 8086 chipsets. The ALU and CU of today's microprocessors are accompanied by a smaller memory unit (cache memory). These days, you can find microprocessors in just about anything: desktop publishing software, power plant control systems, multimeters, medical devices, etc.

  • What is microcontroller??

  • Microcontrollers are single-chip electronic systems that incorporate a processor, memory (RAM, ROM, EPROM), input/output (I/O) pins, and other components. Microcontrollers are miniaturized versions of microcomputers. It is a small, inexpensive electronic component utilized as the primary working device in a variety of other electronic equipment. Microcontrollers are used to automate the device's operation in electrical systems like washing machines, air conditioners, refrigerators, etc. As a result, every single embedded system, such as a microwave oven, washing machine, smart refrigerator, etc., relies on a microcontroller. Microprocessors are used in a wide variety of devices, including those that detect and regulate light, temperature, fire, and other hazards, as well as smart measuring instruments.

    Tasks

    This week has following tasks:

  • Group assignment: browse through the data sheet for your microcontroller compare the performance and development workflows for other architectures Here is our work of group assignments.
  • Individual assignment: 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)
  • Difference between Microprocessor and microcontroller

  • Feature Microcontroller Microprocessor
    Purpose Used in embedded systems for specific tasks Used in general-purpose computing
    Components Contains processor, memory, and I/O on chip Processor only, requires external memory and I/O
    Complexity Typically less complex More complex
    Performance Suited for real-time tasks Suited for high-speed processing
    Cost Generally lower cost Generally higher cost
    Power consumption Typically lower power consumption Typically higher power consumption
    Examples Used in appliances, automotive systems, etc.
    Examples: Arduino, PIC, AVR
    Used in desktops, laptops, servers, etc.
    Examples: Intel Core series, AMD Ryzen series


    Exploring ESP32 Wroom

    In this week I explored ESP32 wroom to send data of temperature and humidity to MATLAB Thingspeak.

    What is ESP32?

    The ESP32 is a cost-effective System on Chip (SoC) microcontroller developed by Espressif Systems, known for creating the popular ESP8266 SoC. It serves as an upgrade to the ESP8266 and is available in single-core and dual-core versions, utilizing Tensilica's 32-bit Xtensa LX6 microprocessor. The ESP32 integrates Wi-Fi and Bluetooth capabilities, similar to the ESP8266. One of its advantages is the inclusion of RF components such as Power Amplifier, Low-Noise Receive Amplifier, Antenna Switch, Filters, and RF Balun, simplifying hardware design by reducing the need for external components.

    raster

    Specifications of ESP32

  • Single or Dual-Core 32-bit LX6 Microprocessor with clock frequency up to 240 MHz.
  • 520 KB of SRAM, 448 KB of ROM and 16 KB of RTC SRAM.
  • Supports 802.11 b/g/n Wi-Fi connectivity with speeds up to 150 Mbps.
  • Support for both Classic Bluetooth v4.2 and BLE specifications.
  • 34 Programmable GPIOs.
  • Up to 18 channels of 12-bit SAR ADC and 2 channels of 8-bit DAC.
  • Serial Connectivity include 4 x SPI, 2 x I2C, 2 x I2S, 3 x UART.
  • Ethernet MAC for physical LAN Communication (requires external PHY).
  • 1 Host controller for SD/SDIO/MMC and 1 Slave controller for SDIO/SPI.
  • Motor PWM and up to 16-channels of LED PWM.
  • Secure Boot and Flash Encryption.
  • Cryptographic Hardware Acceleration for AES, Hash (SHA-2), RSA, ECC and RNG.
  • raster

    DHT11 Specifications

    DHT11 Specifications

    Specification Value
    Operating Voltage 3.3V - 5V
    Humidity Measurement 20% - 80% RH
    Temperature Range 0°C - 50°C
    Temperature Accuracy ±2°C
    Humidity Accuracy ±5% RH
    Refresh Rate 1 Hz
    Interface Digital (Single-Wire)
    Dimensions 12mm x 15.5mm x 5.5mm
    Communication Protocol One-Wire

    Exploring ESP32 and DHT11 for sending data to MATLAB Thingspeak

    Exploring ESP32 and DHT11 for sending data to MATLAB Thingspeak involves using the ESP32 microcontroller along with the DHT11 temperature and humidity sensor to collect data. This data is then sent to MATLAB Thingspeak, a platform for collecting, visualizing, and analyzing IoT data. This project combines hardware and software to create a simple IoT application that can be used for monitoring environmental conditions.



    How to Stream real-time data to ThingSpeak?

    This project is a basic IoT application where temperature data from an ESP-32 microcontroller is sent to the ThingSpeak server. This setup allows users to remotely monitor room temperature from anywhere in the world via the internet. The data transmission is facilitated through the use of ThingSpeak's API. API stands for Application Programming Interface and it is the link between our software application and the website. If we want data from website to use in our application then we need the API.



    raster

    What is thingspeak?

    ThingSpeak is an IoT cloud platform service that enables the visualization of live data streams in the cloud. It allows us to send data from our devices to ThingSpeak, where we can create real-time visualizations of the data.



    Circuit Diagram

    raster

    Procedure

    1.Open Arduino IDE software.



    2. Go to File -> Preferences -> and paste https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json .This will download ESP32 compatibility with Arduino IDE

    raster

    3. From library install ThingSpeak by MathWorks.

    raster

    4. From library install DHT11 by adafruit.

    raster

    5. From internet, download CP2102 driver and extract from zipped folder and update through device manager (since our ESP32 node has cp2102 compatibility), Arduino IDE to detect ESP32.

    rasterraster

    6.Select board as ESP WROOM32: Go to Tools  Board manager ->ESP32 -> ESP32 WRROM DA Module.

    raster

    7.In the program, SSID and password are the hotspot device name and password from which Wifi is connected to both the ESP and laptop/mobile.

    8. Login/Create account in ThingSpeak (MathWorks), using active internet connection. raster raster

    9. Create a new channel and name it according to the sensor. Then create fields (e.g. Temperature -field 1 and Humidity-field 2) raster

    10. Write API key is to be taken from ThingSpeak API Keys of the individual and pasted in the program. raster

    11. In ThingSpeak software MATLAB Visualization and select template required for graph and click create (run program and then visuals will start to appear in the private tab). raster

    ARDUINO PROGRAMMING

    Compiling and uploading program

    rasterraster

    ESP32 and PC is connected to wifi

    #include <WiFi.h>
    #include <HTTPClient.h>
    #include "DHT.h"
    #define DHTPIN 5										
    #define DHTTYPE DHT11
    DHT dht(DHTPIN, DHTTYPE);
    const char* ssid = "*******";
    const char* password = "********";
    // Domain Name with full URL Path for HTTP POST Request
    const char* serverName = "http://api.thingspeak.com/update";
    // write API Key provided by thingspeak
    String apiKey = "*5*R*0*8A*C4****";
    void setup() {
    Serial.begin(115200);										
    WiFi.begin(ssid, password);
    dht.begin();
    Serial.println("Connecting");
    while(WiFi.status() != WL_CONNECTED) {
    delay(2000);
    Serial.print(".");
    							  }
    Serial.println("");
    Serial.print("Connected to WiFi network with IP Address: ");
    Serial.println(WiFi.localIP());
    }
    void loop() {
      if(WiFi.status()== WL_CONNECTED){
      WiFiClient client;
      HTTPClient http;
      delay(10000); // wait for 10 seconds
    //float h = dht.readHumidity();
     float t = dht.readTemperature();
     float h = dht.readHumidity();
     if (isnan(t)) {
       Serial.println(F("Failed to read from DHT sensor!"));
       return;
    			  }
    // Your Domain name with URL path or IP address with path
      http.begin(client, serverName);
    // Specify content-type header
     http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    // Data to send with HTTP POST
    String httpRequestData = "api_key=" + apiKey + "&field1=" + String(t) + "&field2=" + String(h);    
    // Send HTTP POST request
    int httpResponseCode = http.POST(httpRequestData);
    /*
    // If you need an HTTP request with a content type: application/json, use the following:
    http.addHeader("Content-Type", "application/json");
    // JSON data to send with HTTP POST
    String httpRequestData = "{\"api_key\":\"" + apiKey + "\",\"field1\":\"" + String(random(40)) + "\"}";          
    // Send HTTP POST request
    int httpResponseCode = http.POST(httpRequestData);*/		 
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    http.end();
    }
    else {
     Serial.println("WiFi Disconnected");
    				}			
    			}
    
    raster

    Temperature and humidity readings and visualizations

    rasterraster

    Exploring XIAO-RP2040

    I also tried LED blinking of RP2040.I have prepared its PCB in fourth week and then I soldered RP2040 on it.

    raster
    // LED_BUILTIN in connected to pin 25 of the RP2040 chip.
    // It controls the on board LED, at the top-left corner.
    void setup() 
    {
    pinMode(LED_BUILTIN, OUTPUT);
    }
    void loop() 
    {
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    }