# Embedded Programming
In this Week I start in the world of programming. I didn´t know so much about programming. So I start with a easy simulation. For the Code I asked ChatGPT.
## What you need for this:
- For the Simulation I use Wokwi
- For Code generator I use ChatGPT
- A Microcontroller virtual I use ESP32 Website for ESP32
Why I am use ESP32 microcontroller?
Because for my final project I need a controller for the Co2 Sensor and also a commuincation about Wlan or Bluetooth.
At first I looked at the technical Information about the ESP 32-WROOM:
** Power supply voltage (USB) 5V Input/output voltage 3.3V Required operating current min. 500mA SoC ESP32-WROOM 32 Clock frequency range 80MHz / 240MHz RAM 512kB External flash memory 4MB I/O pins 34 Interfaces SPI, I2C, I2S, CAN, UART Wi-Fi protocols 802.11 b/g/n (802.11n up to 150 Mbps) Wi-Fi frequency 2.4 GHz - 2.5 GHz Bluetooth V4.2 - BLE and Classic Bluetooth Wireless antenna PCB **Dimensions 56x28x13mm
Than I looked at the Pinout Diagramm.
Also a interesting is the Xiao from Seed it is smaller but everything I need.
Simulation ESP32 with LED NeoPixel Ring
For the simulation I used a basic ESp32. Not a ESP32-Wroom.
- So I start Wokwi in my browser and take the ESP32 for simulation. So on the right side you can use the components and the sensor. For example I used LED NeoPixel Ring.
- Install the libarie at the left side for FastLED.
- For the Code I asked ChatGPT for a Rainbow code. In my first steps it didn´t worked because of this I changed to an easier code and check out the Pin (change from 4 to 5 to 2) And Pin2 was the right chooice. I Know I must learn to look at the Pinout Diagramm first. And there was a Wokwi problem that the compiling didn’t work because of a busy server. So there was an error, and in my first two hours, I was thinking that it was a message for a wrong code. Sorry, ChatGPT, that I told you this is a wrong code… That was only a busy server. Maybe Wokwi must work on a better UI, or I must sleep more before I start a simulation.
Red Light Simulation:
Prompt for ChatGPT:
"I am working with Wokwi and have installed the FastLED library. I would need a code for FastLED on ESP32 where one LED lights up red. Thank you!"
#include <FastLED.h>
#define LED_PIN 2
#define NUM_LEDS 1
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.clear();
FastLED.setBrightness(255);
leds[0] = CRGB::Red;
FastLED.show();
}
void loop() {
}
Rainbow Light Simulation:
The Prompt I used was a ChatGPT Code:
Can You write me a Code for Rainbow LED with ESP32 with an LED 16 NEO Pixel Ring. I use the FastLED libarie and work in wokwi for a simulatiuon. I changed the LED Pin from 5 to 2 and the delay from 50 to wait. Also I define the BRIGHTNESS and the COLOR_ORDER GRB.
For the Rainbow Code
#include <FastLED.h>
#define LED_PIN 2
#define NUM_LEDS 16
#define BRIGHTNESS 255
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
}
void loop() {
rainbowCycle(10);
}
void rainbowCycle(int wait) {
for (int j = 0; j < 255; j++) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV((i * 256 / NUM_LEDS + j) & 255, 255, 255);
}
FastLED.show();
delay(wait);
}
}
Simulation ESP32 Communication about morse Code to an LED NeoPixel Ring
So for a communication part with the ESP32 I thougt about a morse Code for the LED Ring. This is a little bit complicated but step by step with the code we got it. I got some help from a coworker how did this before. We brought the morse code toghter with the LED Ring Code.
// Morse code dictionary (A-Z and 0-9)
const char* morseDict[36] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--..", // A-Z
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----." // 0-9
};
// Function to get Morse code for a character
const char* charToMorse(char c) {
c = tolower(c); // Convert to lowercase
if (c >= 'a' && c <= 'z') return morseDict[c - 'a'];
if (c >= '0' && c <= '9') return morseDict[c - '0' + 26];
return ""; // No Morse code for this character
}
- In Wokwi you need a ESP32, an LED NeoPixel Ring (16 LEDs), a resistor 330 OHM. Look at the Plan Picture:
Code Explanation:
First of all, you need to define some constants for the pins, the number of LEDs, and the brightness (0 means no brightness, and 255 is the maximum brightness you can set).
Next, you choose a color – in this case, I chose white. The timing for Morse code is important, as it shouldn’t be too fast to properly display the letters and words.
Then, you provide the ESP32 with the Morse code for each letter and number. After that, you need functions to handle the Morse code and the blinking behavior.
Additionally, you’ll need a display to communicate with the ESP32.
Once the code is complete, you can start the simulation. However, I have a few learnings to share. First, you should adjust the timing to a longer dot time. Initially, I used a value of 200 (which was too fast). A dot time of 500 works well for clearly seeing the LEDs blink.
Another important lesson I learned is that sometimes the simulation doesn’t work as expected. This isn’t due to a wrong code or anything like that. It happens because the server can be too busy. Sometimes, I need to restart the simulation multiple times and wait a while before it starts running properly. So, don’t worry if it takes some time or if you experience long waiting periods.
#include <FastLED.h>
#include <ctype.h> // Needed for tolower()
#define LED_PIN 5 // GPIO5 for NeoPixel Data Input (DIN)
#define NUM_LEDS 16 // Number of LEDs in the ring
#define BRIGHTNESS 255 // LED brightness (0-255)
#define COLOR CRGB::White // Morse code color
// Morse code timing in milliseconds
#define DOT_TIME 500 // Duration of a dot (.)
#define DASH_TIME (DOT_TIME * 3) // Duration of a dash (-)
#define LETTER_SPACE (DOT_TIME * 3) // Space between letters
#define WORD_SPACE (DOT_TIME * 7) // Space between words
CRGB leds[NUM_LEDS]; // LED array for FastLED
// Morse code dictionary (A-Z and 0-9)
const char* morseDict[36] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--..", // A-Z
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----." // 0-9
};
// Function to get Morse code for a character
const char* charToMorse(char c) {
c = tolower(c); // Convert to lowercase
if (c >= 'a' && c <= 'z') return morseDict[c - 'a'];
if (c >= '0' && c <= '9') return morseDict[c - '0' + 26];
return ""; // No Morse code for this character
}
// Function to blink LEDs for Morse code
void blinkMorse(const char* morseCode, char character) {
Serial.print(character);
Serial.print(" -> ");
Serial.println(morseCode);
while (*morseCode) {
if (*morseCode == '.') {
Serial.println(" - Dot (.) - LEDs ON");
fill_solid(leds, NUM_LEDS, COLOR);
FastLED.show();
delay(DOT_TIME);
} else if (*morseCode == '-') {
Serial.println(" - Dash (-) - LEDs ON");
fill_solid(leds, NUM_LEDS, COLOR);
FastLED.show();
delay(DASH_TIME);
}
// Turn off LEDs after each signal
Serial.println(" - LEDs OFF");
FastLED.clear();
FastLED.show();
delay(DOT_TIME); // Short pause after a dot or dash
morseCode++;
}
}
// Function to process and display Morse code for a full message
void displayMorseMessage(String message) {
Serial.println("\n--- Morse Code Transmission Start ---\n");
for (int i = 0; i < message.length(); i++) {
char c = message[i];
if (c == ' ') {
Serial.println("\n[ Space between words ]\n");
delay(WORD_SPACE); // Longer pause for word separation
} else {
const char* morseCode = charToMorse(c);
blinkMorse(morseCode, c);
Serial.println("[ Letter space ]");
delay(LETTER_SPACE); // Pause between letters
}
}
Serial.println("\n--- Morse Code Transmission Complete ---\n");
}
void setup() {
Serial.begin(115200);
FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS);
FastLED.clear();
FastLED.show();
Serial.println("Enter a word or sentence to convert into Morse code:");
}
void loop() {
if (Serial.available()) {
String input = Serial.readStringUntil('\n'); // Read input from serial
input.trim(); // Remove leading and trailing spaces
Serial.println("\nProcessing input: " + input);
displayMorseMessage(input);
Serial.println("Ready for new input...");
}
}
Group assignemnt
For the group assignment, we checked out the datasheet of the ESP32-WROOM and programmed an LED to blink using MicroPython and Visual Studio. In our first steps, we used Python, but it didn’t work. Then we found out that the ESP32 doesn’t support Python. I think this was a good learning experience for me—it taught me to do better research to avoid wasting too much time on programming and testing. Also we programmed the same with an ATtiny85 and a RP2040 with the ArduinoIDE.
So watch out my our Group Assignemt: