🔍 DIY Resistor Color Code Scanner
Show a resistor to a tiny camera and watch a colorful screen tell you its value! A fun ESP32-CAM robotics mini project for young makers.
🌈 What Does It Do?
Resistors are tiny parts that control electricity. Instead of numbers, they wear colored stripes (bands). Each color means a number. This scanner uses a camera to look at the stripes, works out the number, and shows the answer on a TFT screen.
Level: MediumTime: 90 minAge: 10+ with adult help
💡 You will learn: how cameras see colors, how resistor codes work, how a microcontroller makes decisions, and how to draw on a display.
🧰 Parts You Need
- ESP32-CAM (AI-Thinker) with OV2640 camera module
- 1.8 inch ST7735 SPI TFT display (the red PCB kind)
- FTDI USB-to-serial programmer (the ESP32-CAM has no USB port)
- Breadboard and jumper wires, 2 alligator clips or a small stand to hold the camera
- 5V power supply (USB power bank or 5V 2A adapter)
- Resistors to test (4-band, gold on the right)
- A plain white sheet of paper as the background
⚠️ Adult help tip: Never connect the board to anything above 5V, and unplug power before changing wires.
🔌 Circuit Connections
The screen uses 6 wires. We keep it simple: RESET and the backlight go straight to 3.3V.
| TFT display pin | ESP32-CAM pin |
|---|---|
| VCC | 3V3 |
| GND | GND |
| SCK / CLK | GPIO 14 |
| SDA / MOSI | GPIO 13 |
| CS | GPIO 15 |
| A0 / DC | GPIO 2 |
| RESET | 3V3 |
| LED / BL (backlight) | 3V3 |
| FTDI programmer | ESP32-CAM |
|---|---|
| 5V | 5V |
| GND | GND |
| TX | U0R |
| RX | U0T |
| For uploading only: connect GPIO 0 to GND. Remove it and press RESET to run. | |
💡 Many 1.8" displays have two silkscreen names per pin. If yours says SCL/SDA, they mean SCK/MOSI.
🛠️ Step-by-Step Build
- Place the parts. Put the ESP32-CAM on the breadboard and the TFT screen beside it.
- Wire the screen using the first table. Double-check every wire before power.
- Mount the camera. Hold it with an alligator clip about 8–10 cm above the table, pointing down at a white paper.
- Install the software. In Arduino IDE open Preferences and add the ESP32 board package URL, then install "esp32" in Boards Manager. Install the libraries Adafruit GFX and Adafruit ST7735.
- Choose the board: Tools → Board → AI Thinker ESP32-CAM.
- Upload. Connect GPIO 0 to GND, press RESET, click Upload, then remove the GPIO 0 wire and press RESET again.
- Scan! Lay a resistor flat across the middle of the paper with the gold band on the right. Nudge it until the colored boxes on screen match your resistor.
💻 The Code
The camera takes a picture, then the code looks along a thin line through the middle. It finds colored stripes, turns each into a number, and calculates the resistance.
#include "esp_camera.h"
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
// TFT: CS=15, DC=2, MOSI=13, SCK=14, RST not used
Adafruit_ST7735 tft(15, 2, 13, 14, -1);
// Camera pins for AI-Thinker ESP32-CAM
#define PWDN 32
#define XCLK 0
#define SIOD 26
#define SIOC 27
#define VSYNC 25
#define HREF 23
#define PCLK 22
const char* names[10]={"Black","Brown","Red","Orange","Yellow","Green","Blue","Violet","Grey","White"};
const uint8_t pal[10][3]={{0,0,0},{120,60,20},{220,30,30},{255,140,0},{240,220,0},{0,160,60},{30,80,220},{150,50,200},{130,130,130},{255,255,255}};
bool startCamera(){
camera_config_t c = {};
c.ledc_channel=LEDC_CHANNEL_0; c.ledc_timer=LEDC_TIMER_0;
c.pin_d0=5; c.pin_d1=18; c.pin_d2=19; c.pin_d3=21;
c.pin_d4=36; c.pin_d5=39; c.pin_d6=34; c.pin_d7=35;
c.pin_xclk=XCLK; c.pin_pclk=PCLK; c.pin_vsync=VSYNC; c.pin_href=HREF;
c.pin_sccb_sda=SIOD; c.pin_sccb_scl=SIOC;
c.pin_pwdn=PWDN; c.pin_reset=-1;
c.xclk_freq_hz=20000000;
c.pixel_format=PIXFORMAT_RGB565;
c.frame_size=FRAMESIZE_QVGA; // 320 x 240
c.jpeg_quality=12; c.fb_count=1;
return esp_camera_init(&c)==ESP_OK;
}
// Turn one pixel color into a band number (-1 = not a band)
int classify(int r,int g,int b){
int mx=max(r,max(g,b)), mn=min(r,min(g,b));
float v=mx/255.0, s=mx? (mx-mn)/(float)mx : 0;
if(v<0.20) return 0; // black
if(s<0.45) return -1; // paper or resistor body
float h;
if(mx==r) h=60.0*(g-b)/(mx-mn);
else if(mx==g) h=60.0*(2+(float)(b-r)/(mx-mn));
else h=60.0*(4+(float)(r-g)/(mx-mn));
if(h<0) h+=360;
if(h<12 || h>=345) return v<0.50 ? 1 : 2; // brown / red
if(h<38) return v<0.55 ? 1 : 3; // brown / orange
if(h<70) return 4; // yellow
if(h<170) return 5; // green
if(h<260) return 6; // blue
if(h<330) return 7; // violet
return 2;
}
void setup(){
tft.initR(INITR_BLACKTAB);
tft.setRotation(1);
tft.fillScreen(ST77XX_WHITE);
tft.setTextColor(ST77XX_BLACK); tft.setTextSize(2);
tft.setCursor(8,50); tft.print("Starting...");
if(!startCamera()){ tft.fillScreen(ST77XX_RED); tft.setCursor(8,50); tft.print("Camera error"); while(1); }
}
void loop(){
camera_fb_t* fb=esp_camera_fb_get();
if(!fb){ delay(100); return; }
int bands[8], n=0, cur=-2, run=0;
for(int x=0;x<320;x++){
long R=0,G=0,B=0;
for(int y=116;y<=124;y++){ // average 9 rows in the middle
int i=(y*320+x)*2;
uint16_t p=(fb->buf[i]<<8)|fb->buf[i+1];
R+=((p>>11)&31)*255/31; G+=((p>>5)&63)*255/63; B+=(p&31)*255/31;
}
int k=classify(R/9,G/9,B/9);
if(k==cur) run++;
else{
if(cur>=0 && run>=5 && n<8) bands[n++]=cur; // stripe must be 5+ pixels wide
cur=k; run=1;
}
}
esp_camera_fb_return(fb);
tft.fillScreen(ST77XX_WHITE);
tft.setTextSize(1); tft.setTextColor(ST77XX_BLUE);
tft.setCursor(6,4); tft.print("RESISTOR SCANNER");
if(n<3){
tft.setTextSize(2); tft.setTextColor(ST77XX_RED);
tft.setCursor(6,50); tft.print("Place resistor");
} else {
for(int i=0;i<min(n,4);i++){ // show colored boxes
tft.fillRect(8+i*36,20,30,30,tft.color565(pal[bands[i]][0],pal[bands[i]][1],pal[bands[i]][2]));
tft.drawRect(8+i*36,20,30,30,ST77XX_BLACK);
}
float ohms=(bands[0]*10+bands[1]);
for(int m=0;m<bands[2];m++) ohms*=10;
tft.setTextColor(ST77XX_BLACK); tft.setTextSize(2); tft.setCursor(8,66);
if(ohms>=1000000) { tft.print(ohms/1000000.0,1); tft.print(" M"); }
else if(ohms>=1000) { tft.print(ohms/1000.0,1); tft.print(" k"); }
else { tft.print((int)ohms); tft.print(" "); }
tft.print((char)0xF4); // ohm-like symbol; may show differently
tft.setTextSize(1); tft.setCursor(8,95);
tft.print(names[bands[0]]); tft.print(" ");
tft.print(names[bands[1]]); tft.print(" ");
tft.print(names[bands[2]]);
}
delay(700);
}
💡 Tuning tip: If brown looks red or orange looks yellow, change the v<0.50 and h<38 numbers a little. Every room light is different!
🎨 Resistor Color Code Cheat Sheet
| Color | Digit | Multiplier |
|---|---|---|
| Black | 0 | ×1 |
| Brown | 1 | ×10 |
| Red | 2 | ×100 |
| Orange | 3 | ×1,000 |
| Yellow | 4 | ×10,000 |
| Green | 5 | ×100,000 |
| Blue | 6 | ×1,000,000 |
| Violet | 7 | ×10,000,000 |
| Grey | 8 | ×100,000,000 |
| White | 9 | ×1,000,000,000 |
| Gold | ±5% tolerance (always on the right end) | |
Example: Red – Violet – Orange – Gold = 2, 7, ×1,000 = 27,000 Ω = 27 kΩ.
❓ Troubleshooting & FAQ
Screen is white or blank? Check 3V3, GND, and that RESET and LED are both on 3V3. If your display is ILI9341 instead of ST7735, swap the library and constructor.
"Camera error" appears? Reseat the camera ribbon cable, and use a good 5V supply (weak USB ports cause errors).
Wrong colors? Use bright, even light with no shadows, a white paper background, and keep the resistor sharp and centered. This scanner works best on 4-band resistors, with gold on the right.
Upload fails? Make sure GPIO 0 is connected to GND while uploading, and press RESET right before it starts.
🚀 Challenges: show the tolerance too, add a buzzer that beeps when a resistor is found, or draw the live camera picture on the screen.
Comments
Post a Comment