// ===================================================
// 7-Segment Servo Display (1 Servo per Segment)
// ESP32 + Wokwi
// ===================================================
#include <ESP32Servo.h>
// -------- Servo Pins (ESP32 GPIOs) --------
#define SERVO_A 16
#define SERVO_B 4
#define SERVO_C 2
#define SERVO_D 15
#define SERVO_E 19
#define SERVO_F 18
#define SERVO_G 5
// -------- Servo Angles --------
const int SEG_OFF = 0; // segment OFF position
const int SEG_ON = 90; // segment ON position
// -------- Servo Objects --------
Servo segA;
Servo segB;
Servo segC;
Servo segD;
Servo segE;
Servo segF;
Servo segG;
// -------- 7-Segment Digit Table --------
// Order: A B C D E F G
const bool DIGIT_SEGMENTS[10][7] = {
{1,1,1,1,1,1,0}, // 0
{0,1,1,0,0,0,0}, // 1
{1,1,0,1,1,0,1}, // 2
{1,1,1,1,0,0,1}, // 3
{0,1,1,0,0,1,1}, // 4
{1,0,1,1,0,1,1}, // 5
{1,0,1,1,1,1,1}, // 6
{1,1,1,0,0,0,0}, // 7
{1,1,1,1,1,1,1}, // 8
{1,1,1,1,0,1,1} // 9
};
// -------- Helper Function --------
void setSegment(Servo &servo, bool on) {
servo.write(on ? SEG_ON : SEG_OFF);
}
// -------- Display Digit --------
void showDigit(int digit) {
if (digit < 0 || digit > 9) return;
setSegment(segA, DIGIT_SEGMENTS[digit][0]);
setSegment(segB, DIGIT_SEGMENTS[digit][1]);
setSegment(segC, DIGIT_SEGMENTS[digit][2]);
setSegment(segD, DIGIT_SEGMENTS[digit][3]);
setSegment(segE, DIGIT_SEGMENTS[digit][4]);
setSegment(segF, DIGIT_SEGMENTS[digit][5]);
setSegment(segG, DIGIT_SEGMENTS[digit][6]);
}
// -------- Setup --------
void setup() {
Serial.begin(115200);
Serial.println("7-Segment Servo Display Ready");
Serial.println("Send digits 0–9 via Serial Monitor");
segA.attach(SERVO_A);
segB.attach(SERVO_B);
segC.attach(SERVO_C);
segD.attach(SERVO_D);
segE.attach(SERVO_E);
segF.attach(SERVO_F);
segG.attach(SERVO_G);
showDigit(0); // default display
}
// -------- Loop --------
void loop() {
if (Serial.available()) {
char ch = Serial.read();
if (ch >= '0' && ch <= '9') {
showDigit(ch - '0');
}
}
}