This Morse code trainer helps learners master Morse code through interactive practice sessions on an ESP32 microcontroller. The trainer displays random letters and sentences on an OLED screen, plays Morse code patterns through a piezo buzzer, and uses a tactile button for user interaction. The guide includes a complete wiring diagram, parts list, and step-by-step assembly instructions to connect the OLED display, button, buzzer, DHT11 sensor, and infrared obstacle sensor to the ESP32.
The firmware implements a full training system with 50 pre-loaded sentences, automatic difficulty progression through letters and sentence-based quizzes, and debounced button input for reliable interaction. Builders will learn how to integrate multiple sensors and outputs on a single microcontroller while creating a practical educational tool for Morse code practice.
Wiring diagram

Gather all the parts
Assemble it in 5 steps
1. Connect the OLED screen
With USB unplugged, connect VCC to 3V3, GND to GND, SDA to GPIO21 (data), and SCL to GPIO22 (clock). Follow the printed pin labels.
2. Connect the single button
With USB unplugged, remove the second button. Connect the remaining button between GPIO25 and GND, using legs from opposite contact pairs.
3. Connect the passive piezo buzzer
With USB unplugged, connect SIG (or +) to GPIO27 for sound, and GND (or -) to GND. Use a passive piezo. It sounds continuously while the obstacle screen detects an object, and stops when the object clears or you leave that screen.
4. Connect the DHT11 module
Unplug USB. Use a 3.3 V compatible DHT11 module with an onboard data pull-up. Follow its pin labels. Connect VCC to 3V3, GND to GND, and DATA to GPIO33. Tap to move through the main menu and hold to select DHT11. Hold again to return.
5. Connect the infrared obstacle sensor
With USB unplugged, use a three-pin FC-51 rated for 3.3 V. Follow the printed labels and connect VCC to 3V3 (not 5V), GND to GND, and OUT to GPIO32 (detection signal). Do not connect an unidentified module. Tap to highlight Obstacle in the main menu, then hold to open it. Hold again to return.
Review all connections
1. Connections between "oled" and "ESP32"
2. Connections between "dot" and "ESP32"
3. Connections between "piezo" and "ESP32"
4. Connections between "climate" and "ESP32"
5. Connections between "obstacle" and "ESP32"
Deploy the firmware
#include <DHT.h>
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128,64,&Wire,-1);
constexpr int piezoPin=27;
void playSymbol(bool isDot){tone(piezoPin,2400,isDot?70:210);}
const char* morse[]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
struct Key {
int pin; bool raw, stable; uint32_t changed;
bool tap(uint32_t now){
bool v=!digitalRead(pin);
if(v!=raw){raw=v;changed=now;}
if(v!=stable && now-changed>=25){stable=v;return v;}
return false;
}
};
Key button{25,false,false,0};
const char* const sentences[]={
"I AM OK","WE CAN GO","RUN FAST","I SEE YOU","GO HOME","BE KIND",
"WE DID IT","STAY SAFE","KEEP GOING","I CAN RUN","WE CAN WIN","IT IS HOT",
"I AM HERE","IT IS COLD","LOOK UP","COME HERE","SIT DOWN","TAKE CARE",
"I CAN HELP","WE ARE OK","TRY AGAIN","WAKE UP","IT IS FUN","WE CAN TRY",
"I SEE RAIN","I AM READY","BIRDS FLY","DOGS RUN","CATS NAP","FISH SWIM",
"IT CAN WAIT","WE CAN REST","I LIKE TEA","I SEE STARS","I FEEL GOOD","WE ARE HERE",
"GO SLOW","STOP HERE","LOOK LEFT","TURN RIGHT","READ A BOOK","DRINK WATER",
"OPEN IT","CLOSE IT","HOLD ON","LET IT GO","DO YOUR BEST","I CAN JUMP",
"THE SUN SET","WE MADE IT"
};
constexpr int sentenceCount=sizeof(sentences)/sizeof(sentences[0]);
constexpr int letters=26, sentencesPerQuiz=6, rounds=letters+sentencesPerQuiz;
static_assert(sentenceCount==50,"Use 50 sentence prompts");
char alphabet[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
uint8_t sentenceOrder[sentenceCount];
int sentencePosition=sentenceCount;
int roundIndex=0,pos,len;
char answer[41],entry[41];
char letterPrompt[2];
const char* prompt;
template<class T> void shuffle(T* items,int count){
for(int i=count-1;i>0;--i){int j=random(i+1);T x=items[i];items[i]=items[j];items[j]=x;}
}
void newQuiz(){
shuffle(alphabet,letters);
}
const char* nextSentence(){
// Use the whole deck before reshuffling, even midway through a quiz.
if(sentencePosition==sentenceCount){
int previous=sentenceOrder[sentenceCount-1];
for(int i=0;i<sentenceCount;++i)sentenceOrder[i]=i;
shuffle(sentenceOrder,sentenceCount);
if(sentenceOrder[0]==previous){auto x=sentenceOrder[0];sentenceOrder[0]=sentenceOrder[1];sentenceOrder[1]=x;}
sentencePosition=0;
}
return sentences[sentenceOrder[sentencePosition++]];
}
bool result=false,finished=false;
int score=0;
void draw(){
oled.clearDisplay();oled.setTextSize(1);oled.setCursor(0,0);
if(finished){
oled.println("Practice complete!");
oled.print(score);oled.print('/');oled.println(rounds);
oled.println("Press: menu");
} else {
oled.println(result?(strcmp(answer,entry)==0?"Correct!":"Incorrect"):
(roundIndex>=letters?"Send the sentence":"Send the letter"));
oled.setTextSize(2);oled.setCursor(0,12);oled.print(prompt);
oled.setTextSize(1);oled.setCursor(0,32);
if(result){
oled.print("Code: ");
for(const char* c=prompt;*c;c++){
if(*c==' ')oled.print(F("/ "));
else {oled.print(morse[*c-'A']);oled.print(' ');}
}
oled.setCursor(0,56);oled.print("Press: next");
} else {
oled.println(entry);
oled.println("Tap dot / hold dash");
oled.print(pos);oled.print('/');oled.print(len);
oled.print(" symbols");
// Show the code for the letter currently being entered.
int remaining=pos;
for(const char* c=prompt;*c;++c){
if(*c==' ')continue;
const char* symbols=morse[*c-'A'];
int count=strlen(symbols);
if(remaining<count){
oled.setCursor(0,56);oled.print("Hint ");oled.print(*c);
oled.print(": ");oled.print(symbols);
break;
}
remaining-=count;
}
}
}
oled.display();
}
void question(){
if(roundIndex<letters){letterPrompt[0]=alphabet[roundIndex];prompt=letterPrompt;}
else prompt=nextSentence();
answer[0]=0;
for(const char* c=prompt;*c;c++)if(*c!=' ')strcat(answer,morse[*c-'A']);
len=strlen(answer);pos=0;entry[0]=0;result=false;draw();
}
enum class Mode { Menu, Quiz, Translate, Sensor, Obstacle };
Mode mode=Mode::Menu;
String decoded, code;
uint32_t pressedAt;
int selection=0;
void menu(){
mode=Mode::Menu;
oled.clearDisplay();oled.setTextSize(1);oled.setCursor(0,0);
oled.println("Tap: move Hold: open");
const char* names[]={"Quiz","Translator","DHT11","Obstacle"};
for(int i=0;i<4;++i){oled.print(i==selection?"> ":" ");oled.println(names[i]);}
oled.display();
}
void drawTranslator(){
oled.clearDisplay();oled.setTextSize(1);oled.setCursor(0,0);
oled.println("Morse -> text");
oled.print("Code: ");oled.println(code);
oled.println(decoded);
oled.setCursor(0,40);
oled.println("Tap dot / 0.3s dash");
oled.println("1s: letter or space");
oled.println("2.5s: menu");
oled.display();
}
void translateKey(int key,bool held){
if(held && key==1){menu();return;}
if(held){
char letter=code.isEmpty()?' ':'?';
for(int i=0;i<letters;++i)if(code==morse[i]){letter='A'+i;break;}
if(decoded.length()>=42)decoded.remove(0,1);
decoded+=letter;code="";
}else if(code.length()<5){code+=key==0?'.':'-';playSymbol(key==0);}
drawTranslator();
}
constexpr int sensorPin=33;
constexpr int obstaclePin=32;
int lastObstacle=-1;
void runObstacle(){
int detected=digitalRead(obstaclePin)==LOW;
if(detected==lastObstacle)return;
lastObstacle=detected;
oled.clearDisplay();oled.setTextSize(1);oled.setCursor(0,0);
oled.println("IR obstacle sensor");
oled.println(detected?"Obstacle detected":"No obstacle");
oled.println("Hold: menu");oled.display();
if(detected)tone(piezoPin, 2400);
else noTone(piezoPin);
}
DHT climate(sensorPin,DHT11);
uint32_t lastClimateRead;
String climateText;
void drawSensor(){
oled.clearDisplay();oled.setTextSize(1);oled.setCursor(0,0);
oled.println("DHT11 readings");
oled.println(climateText);
oled.println("Hold: menu");oled.display();
}
void runSensor(uint32_t now){
if(now-lastClimateRead<2000) return;
lastClimateRead=now;
float h=climate.readHumidity(), t=climate.readTemperature();
String next="Read failed";
if(!isnan(h) && !isnan(t)) next=String("Temp: ")+String(t,0)+" C\nHumidity: "+String(h,0)+" %";
if(next!=climateText){climateText=next;drawSensor();}
}
void setup(){
Serial.begin(115200);
climate.begin();
pinMode(obstaclePin,INPUT_PULLUP);
pinMode(piezoPin,OUTPUT);digitalWrite(piezoPin,LOW);
pinMode(button.pin,INPUT_PULLUP);
Wire.begin(21,22);
delay(100);
uint8_t oledAddress=0;
for(uint8_t address=0x3C;address<=0x3D;++address){
Wire.beginTransmission(address);
if(Wire.endTransmission()==0){oledAddress=address;break;}
}
if(!oledAddress){
Serial.println("OLED not responding at 0x3C or 0x3D. Check power and SDA=21, SCL=22.");
while(true)delay(100);
}
Serial.printf("OLED responded at 0x%02X\n",oledAddress);
if(!oled.begin(SSD1306_SWITCHCAPVCC,oledAddress,false,false)){
Serial.println("OLED initialization failed.");
while(true)delay(100);
}
oled.setTextColor(SSD1306_WHITE);randomSeed(esp_random());menu();
}
void loop(){
uint32_t now=millis();
bool wasDown=button.stable;
if(button.tap(now))pressedAt=now;
if(wasDown && !button.stable){
uint32_t duration=now-pressedAt;
bool held=duration>=300;
switch(mode){
case Mode::Menu:
if(!held){selection=(selection+1)%4;menu();}
else if(selection==0){mode=Mode::Quiz;newQuiz();question();}
else if(selection==1){mode=Mode::Translate;decoded="";code="";drawTranslator();}
else if(selection==2){mode=Mode::Sensor;lastClimateRead=now;climateText="Reading...";drawSensor();}
else {mode=Mode::Obstacle;lastObstacle=-1;}
break;
case Mode::Translate:
if(duration>=2500)menu();
else if(duration>=1000)translateKey(0,true);
else translateKey(held?1:0,false);
break;
case Mode::Sensor: case Mode::Obstacle:
if(held){if(mode==Mode::Obstacle)noTone(piezoPin);menu();}
break;
case Mode::Quiz:
if(finished){finished=false;score=0;roundIndex=0;menu();}
else if(result){if(++roundIndex==rounds){finished=true;draw();}else question();}
else {
playSymbol(!held);entry[pos++]=held?'-':'.';entry[pos]=0;
if(pos==len){result=true;if(strcmp(answer,entry)==0)score++;}
draw();
}
break;
}
}
if(mode==Mode::Obstacle)runObstacle();
else if(mode==Mode::Sensor)runSensor(now);
delay(1);
}Remix this project
Make it yours in one click
Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.




