CNAP/Component Guides38 components

Component Guides

Wiring tables, example sketches, and step-by-step instructions for every component in the playground palette.

LEDs & Displays

πŸ’‘

LED

wokwi-led

A single LED β€” the classic first project. Wire it through a resistor and blink it.

Wiring

Component pinArduino pin
Anode (+)Pin 13 (via 220 Ξ© resistor)
Cathode (βˆ’)GND

Sketch

// LED Blink β€” turns an LED on and off every second
const int LED_PIN = 13;

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_PIN, HIGH);  // LED on
  delay(1000);
  digitalWrite(LED_PIN, LOW);   // LED off
  delay(1000);
}

How to test in the playground

  1. Drag a Resistor (220 Ξ©) and an LED onto the canvas.
  2. Wire: Arduino Pin 13 β†’ Resistor Pin 1 β†’ Resistor Pin 2 β†’ LED Anode (+).
  3. Wire: LED Cathode (βˆ’) β†’ Arduino GND.
  4. Paste the sketch and click Run β€” the LED should blink once per second.

Note: Always use a current-limiting resistor (220–1 kΞ©) in series with the LED.

🌈

RGB LED

wokwi-rgb-led

A common-cathode RGB LED with separate red, green, and blue pins. Drive each with PWM for full colour mixing.

Wiring

Component pinArduino pin
R (Red)Pin 9 (PWM)
G (Green)Pin 10 (PWM)
B (Blue)Pin 11 (PWM)
COM (Cathode)GND

Sketch

// RGB LED β€” cycles through red β†’ green β†’ blue β†’ white
const int R = 9, G = 10, B = 11;

void setColor(int r, int g, int b) {
  analogWrite(R, r);
  analogWrite(G, g);
  analogWrite(B, b);
}

void setup() {
  pinMode(R, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(B, OUTPUT);
}

void loop() {
  setColor(255, 0,   0);   delay(800);  // red
  setColor(0,   255, 0);   delay(800);  // green
  setColor(0,   0,   255); delay(800);  // blue
  setColor(255, 255, 255); delay(800);  // white
  setColor(255, 165, 0);   delay(800);  // orange
}

How to test in the playground

  1. Drag an RGB LED onto the canvas.
  2. Wire R β†’ Pin 9, G β†’ Pin 10, B β†’ Pin 11 (each via a 100 Ξ© resistor).
  3. Wire COM β†’ GND.
  4. Paste the sketch and click Run β€” the LED should cycle through colours.

Note: Use analogWrite (0–255) for brightness. Add a 100 Ξ© resistor on each colour leg.

πŸ“Š

LED Bar Graph

wokwi-led-bar-graph

A 10-segment LED bar graph. Drive each segment individually for level-meter or chaser effects.

Wiring

Component pinArduino pin
Anode 1–10 (A1–A10)Pins 2–11 (each via 220 Ξ© resistor)
Cathode 1–10 (C1–C10)GND (all tied together)

Sketch

// LED Bar Graph β€” Knight Rider chaser
const int NUM_LEDS = 10;
const int PINS[NUM_LEDS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(PINS[i], OUTPUT);
  }
}

void loop() {
  // Sweep forward
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(PINS[i], HIGH);
    delay(60);
    digitalWrite(PINS[i], LOW);
  }
  // Sweep backward
  for (int i = NUM_LEDS - 2; i > 0; i--) {
    digitalWrite(PINS[i], HIGH);
    delay(60);
    digitalWrite(PINS[i], LOW);
  }
}

How to test in the playground

  1. Drag a LED Bar Graph and 10 resistors (220 Ξ©) onto the canvas.
  2. Wire A1 β†’ Resistor β†’ Pin 2, A2 β†’ Resistor β†’ Pin 3, … A10 β†’ Resistor β†’ Pin 11.
  3. Tie all cathodes (C1–C10) to GND.
  4. Paste the sketch and click Run β€” a dot chases back and forth.
πŸ’«

LED Ring

wokwi-led-ring

A ring of WS2812B NeoPixels addressable over a single data wire. Requires the FastLED or Adafruit NeoPixel library.

Wiring

Component pinArduino pin
GNDGND
VCC5V
DIN (Data In)Pin 6

Sketch

// LED Ring β€” spinning rainbow chase (FastLED)
#include <FastLED.h>

#define NUM_LEDS  16
#define DATA_PIN  6

CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(80);
}

void loop() {
  static uint8_t hue = 0;
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CHSV(hue + (i * 256 / NUM_LEDS), 255, 255);
  }
  FastLED.show();
  hue++;
  delay(20);
}

How to test in the playground

  1. Drag an LED Ring onto the canvas.
  2. Wire VCC β†’ 5V, GND β†’ GND, DIN β†’ Pin 6.
  3. Paste the sketch and click Run β€” a rainbow pattern spins around the ring.

Note: The wokwi-led-ring defaults to 16 LEDs. Change NUM_LEDS to match if you resize it.

πŸ”΅

NeoPixel

wokwi-neopixel

A single WS2812B NeoPixel. Addressable via one data pin β€” no resistor needed.

Wiring

Component pinArduino pin
VDD (VCC)5V
VSS (GND)GND
DIN (Data In)Pin 6

Sketch

// Single NeoPixel β€” colour cycle
#include <Adafruit_NeoPixel.h>

#define PIN   6
#define NUMPIXELS 1

Adafruit_NeoPixel pixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  pixel.begin();
  pixel.setBrightness(100);
}

void loop() {
  uint32_t colors[] = {
    pixel.Color(255, 0,   0),   // red
    pixel.Color(0,   255, 0),   // green
    pixel.Color(0,   0,   255), // blue
    pixel.Color(255, 255, 0),   // yellow
  };
  for (auto c : colors) {
    pixel.setPixelColor(0, c);
    pixel.show();
    delay(600);
  }
}

How to test in the playground

  1. Drag a NeoPixel onto the canvas.
  2. Wire VDD β†’ 5V, VSS β†’ GND, DIN β†’ Pin 6.
  3. Paste the sketch and click Run β€” the pixel cycles through colours.
🌈

NeoPixel Matrix

wokwi-neopixel-matrix

A matrix of WS2812B NeoPixels arranged in rows and columns. Default is 8Γ—8 (64 pixels). Addressable via a single data wire using the Adafruit NeoPixel library.

Wiring

Component pinArduino pin
VCC5V
GNDGND
DIN (Data In)Pin 6

Sketch

// NeoPixel Matrix 8Γ—8 β€” diagonal rainbow wave
#include <Adafruit_NeoPixel.h>

#define DATA_PIN   6
#define ROWS       8
#define COLS       8
#define NUMPIXELS  (ROWS * COLS)

Adafruit_NeoPixel matrix(NUMPIXELS, DATA_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  matrix.begin();
  matrix.setBrightness(60);
}

void loop() {
  static uint16_t offset = 0;
  for (int row = 0; row < ROWS; row++) {
    for (int col = 0; col < COLS; col++) {
      uint16_t hue = offset + (uint16_t)(row + col) * 4096;
      uint32_t color = matrix.gamma32(matrix.ColorHSV(hue, 255, 200));
      matrix.setPixelColor(row * COLS + col, color);
    }
  }
  matrix.show();
  offset += 512;
  delay(30);
}

How to test in the playground

  1. Drag a NeoPixel Matrix onto the canvas.
  2. Wire VCC β†’ 5V, GND β†’ GND, DIN β†’ Pin 6.
  3. Paste the sketch (requires Adafruit NeoPixel library) and click Run.
  4. A diagonal rainbow wave should scroll across the 8Γ—8 grid.

Note: Pixels are indexed row-major: pixel 0 is top-left, pixel (rowsΓ—colsβˆ’1) is bottom-right. Lower setBrightness (40–80) to avoid heavy current draw on larger matrices.

7️⃣

7-Segment

wokwi-7segment

A single-digit 7-segment common-cathode display. Each segment (A–G) is driven by a separate pin.

Wiring

Component pinArduino pin
Segment APin 2
Segment BPin 3
Segment CPin 4
Segment DPin 5
Segment EPin 6
Segment FPin 7
Segment GPin 8
COM (both)GND

Sketch

// 7-Segment β€” counts 0 to 9
// Segment order: A  B  C  D  E  F  G
const int SEG[7] = {2, 3, 4, 5, 6, 7, 8};

// Common-cathode digit patterns (1 = segment ON)
const byte DIGITS[10] = {
  0b0111111, // 0
  0b0000110, // 1
  0b1011011, // 2
  0b1001111, // 3
  0b1100110, // 4
  0b1101101, // 5
  0b1111101, // 6
  0b0000111, // 7
  0b1111111, // 8
  0b1101111, // 9
};

void showDigit(int d) {
  for (int i = 0; i < 7; i++) {
    digitalWrite(SEG[i], (DIGITS[d] >> i) & 1);
  }
}

void setup() {
  for (int p : SEG) pinMode(p, OUTPUT);
}

void loop() {
  for (int d = 0; d <= 9; d++) {
    showDigit(d);
    delay(600);
  }
}

How to test in the playground

  1. Drag a 7-Segment display onto the canvas.
  2. Wire segments A–G to pins 2–8 (each via a 220 Ξ© resistor).
  3. Wire both COM pins to GND.
  4. Paste the sketch and click Run β€” digits 0–9 count up.

Note: The decimal point (DP) is not wired in this sketch β€” connect it to another pin if needed.

πŸ–₯️

LCD 1602

wokwi-lcd1602

A 16Γ—2 character LCD. Wire in 4-bit mode and use the LiquidCrystal library.

Wiring

Component pinArduino pin
VSS (GND)GND
VDD (5V)5V
V0 (Contrast)GND (or potentiometer wiper)
RSPin 12
RWGND
E (Enable)Pin 11
D4Pin 5
D5Pin 4
D6Pin 3
D7Pin 2
A (Backlight +)5V via 220 Ξ© resistor
K (Backlight βˆ’)GND

Sketch

// LCD 1602 β€” Hello World + uptime counter
#include <LiquidCrystal.h>

// RS, E, D4, D5, D6, D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);
  lcd.print("Hello, World!");
}

void loop() {
  lcd.setCursor(0, 1);
  lcd.print("Time: ");
  lcd.print(millis() / 1000);
  lcd.print("s  ");
  delay(500);
}

How to test in the playground

  1. Drag an LCD 1602 onto the canvas.
  2. Follow the wiring table β€” tie RW to GND and V0 to GND for max contrast.
  3. Paste the sketch and click Run β€” row 1 shows 'Hello, World!', row 2 counts seconds.

Note: Tie V0 to a potentiometer wiper for adjustable contrast.

πŸ–₯️

LCD 2004

wokwi-lcd2004

A 20Γ—4 character LCD β€” same 16-pin interface as the 1602, just taller.

Wiring

Component pinArduino pin
VSS (GND)GND
VDD (5V)5V
V0 (Contrast)GND
RSPin 12
RWGND
E (Enable)Pin 11
D4Pin 5
D5Pin 4
D6Pin 3
D7Pin 2
A (Backlight +)5V via 220 Ξ© resistor
K (Backlight βˆ’)GND

Sketch

// LCD 2004 β€” fills all four rows
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(20, 4);
  lcd.setCursor(0, 0); lcd.print("Row 0: Hello!");
  lcd.setCursor(0, 1); lcd.print("Row 1: Arduino");
  lcd.setCursor(0, 2); lcd.print("Row 2: LCD 2004");
  lcd.setCursor(0, 3); lcd.print("Row 3: 20 x 4");
}

void loop() {}

How to test in the playground

  1. Drag an LCD 2004 onto the canvas.
  2. Wire it identically to the LCD 1602 (same 16-pin pinout).
  3. Change lcd.begin() to (20, 4) β€” the library handles the rest.
  4. Paste the sketch and click Run β€” all four rows should display text.
πŸ–₯️

ILI9341 TFT

wokwi-ili9341

A 240Γ—320 colour TFT display using the ILI9341 controller over SPI. Use the Adafruit ILI9341 library.

Wiring

Component pinArduino pin
VCC3.3V
GNDGND
CSPin 10
RSTPin 9
D/CPin 8
MOSIPin 11 (SPI MOSI)
SCKPin 13 (SPI SCK)
LED (Backlight)3.3V via 100 Ξ© resistor
MISOPin 12 (SPI MISO)

Sketch

// ILI9341 TFT β€” draws coloured shapes
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>

#define TFT_CS  10
#define TFT_RST  9
#define TFT_DC   8

Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);

void setup() {
  tft.begin();
  tft.setRotation(1);          // landscape
  tft.fillScreen(ILI9341_BLACK);
  tft.fillRect(10, 10, 100, 60, ILI9341_RED);
  tft.fillCircle(200, 100, 40, ILI9341_CYAN);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(2);
  tft.setCursor(20, 90);
  tft.print("ILI9341!");
}

void loop() {}

How to test in the playground

  1. Drag an ILI9341 TFT onto the canvas.
  2. Wire SPI pins (MOSI→11, SCK→13, MISO→12) plus CS→10, RST→9, D/C→8.
  3. Power from 3.3V (not 5V β€” the display is not 5V tolerant).
  4. Paste the sketch and click Run β€” a red rectangle, cyan circle, and text appear.

Note: This display runs on 3.3V logic. On a 5V Arduino, add a logic-level shifter on MOSI, SCK, CS, RST, and D/C.

πŸ“Ί

SSD1306 OLED

wokwi-ssd1306

A 128Γ—64 monochrome OLED display using IΒ²C (or SPI). Perfect for compact status readouts.

Wiring

Component pinArduino pin
VIN3.3V or 5V
GNDGND
SDA (DATA)A4 (SDA)
SCL (CLK)A5 (SCL)

Sketch

// SSD1306 OLED β€” Hello World over I2C
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1  // share Arduino reset pin

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(2);
  display.setCursor(0, 0);
  display.println("Hello!");
  display.setTextSize(1);
  display.println("SSD1306 OLED");
  display.println("128 x 64 pixels");
  display.display();
}

void loop() {}

How to test in the playground

  1. Drag an SSD1306 OLED onto the canvas.
  2. Wire SDA β†’ A4, SCL β†’ A5, VIN β†’ 3.3V (or 5V), GND β†’ GND.
  3. The default IΒ²C address is 0x3C (some modules use 0x3D).
  4. Paste the sketch and click Run β€” 'Hello!' should appear on the screen.

Passive Components

〰️

Resistor

wokwi-resistor

A through-hole resistor used for current limiting, pull-up/pull-down networks, and voltage dividers.

Wiring

Component pinArduino pin
Pin 1Signal source (e.g. Arduino pin)
Pin 2Load (e.g. LED anode)

Sketch

// Resistor as pull-up β€” read a button with internal pull-up disabled
// (external 10 kΞ© pull-up resistor on Pin 2)
const int BTN = 2;

void setup() {
  pinMode(BTN, INPUT);   // NOT INPUT_PULLUP β€” we use the external resistor
  Serial.begin(9600);
}

void loop() {
  int state = digitalRead(BTN);
  Serial.println(state == HIGH ? "Released" : "Pressed");
  delay(100);
}

How to test in the playground

  1. Drag a Resistor onto the canvas.
  2. Set its value attribute (e.g. '220' for 220 Ξ©, '10000' for 10 kΞ©).
  3. Connect it in series between the signal source and the load.
  4. Paste the sketch and open the Serial Monitor to see button state.

Note: The default attribute value is '1000' (1 kΞ©). Click the component to change it.

Buttons & Switches

πŸ”˜

Pushbutton

wokwi-pushbutton

A large through-hole pushbutton. Pressing it connects the left pins to the right pins.

Wiring

Component pinArduino pin
1.l (left, top)Pin 2
2.l (left, bottom)GND

Sketch

// Pushbutton β€” toggle an LED on each press
const int BTN = 2;
const int LED = 13;
bool ledState = false;
bool lastBtn = HIGH;

void setup() {
  pinMode(BTN, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
}

void loop() {
  bool btn = digitalRead(BTN);
  if (btn == LOW && lastBtn == HIGH) {  // falling edge = press
    ledState = !ledState;
    digitalWrite(LED, ledState);
    delay(20);  // debounce
  }
  lastBtn = btn;
}

How to test in the playground

  1. Drag a Pushbutton onto the canvas.
  2. Wire pin 1.l β†’ Pin 2, pin 2.l β†’ GND (the built-in pull-up handles the rest).
  3. Also wire Pin 13 β†’ LED β†’ GND via a 220 Ξ© resistor.
  4. Paste the sketch and click Run β€” press the button to toggle the LED.

Note: Using INPUT_PULLUP means the button reads HIGH when released and LOW when pressed.

πŸ”˜

Pushbutton 6mm

wokwi-pushbutton-6mm

A compact 6 mm SMD-style pushbutton β€” same electrical behaviour as the larger pushbutton.

Wiring

Component pinArduino pin
1.lPin 2
2.lGND

Sketch

// 6mm Pushbutton β€” print message on press
const int BTN = 2;

void setup() {
  pinMode(BTN, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Press the button!");
}

void loop() {
  if (digitalRead(BTN) == LOW) {
    Serial.println("Button pressed!");
    delay(200);  // simple debounce
  }
}

How to test in the playground

  1. Drag a Pushbutton 6mm onto the canvas.
  2. Wire 1.l β†’ Pin 2, 2.l β†’ GND.
  3. Paste the sketch and open the Serial Monitor β€” press the button to see the message.
πŸ”€

Slide Switch

wokwi-slide-switch

A 3-pin SPDT slide switch. The common pin connects to either Pin 1 or Pin 3 depending on switch position.

Wiring

Component pinArduino pin
Pin 1 (left position)GND
Pin 2 (Common)Pin 4
Pin 3 (right position)5V

Sketch

// Slide Switch β€” read switch position
const int SW = 4;

void setup() {
  pinMode(SW, INPUT);
  Serial.begin(9600);
}

void loop() {
  int state = digitalRead(SW);
  Serial.println(state == HIGH ? "Switch: ON" : "Switch: OFF");
  delay(300);
}

How to test in the playground

  1. Drag a Slide Switch onto the canvas.
  2. Wire: Pin 1 β†’ GND, Common (Pin 2) β†’ Pin 4, Pin 3 β†’ 5V.
  3. Paste the sketch and open the Serial Monitor.
  4. Click the switch on the canvas to toggle it β€” the Serial Monitor shows the state.
πŸŽ›οΈ

DIP Switch 8

wokwi-dip-switch-8

An 8-switch DIP package. Each switch independently connects its A pin to its B pin when closed.

Wiring

Component pinArduino pin
1B–8BPins 2–9
1A–8AGND (all tied together)

Sketch

// DIP Switch 8 β€” read all 8 switches as a byte
const int PINS[8] = {2, 3, 4, 5, 6, 7, 8, 9};

void setup() {
  for (int p : PINS) pinMode(p, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  byte val = 0;
  for (int i = 0; i < 8; i++) {
    if (digitalRead(PINS[i]) == LOW) {  // LOW = switch closed
      val |= (1 << i);
    }
  }
  Serial.print("DIP = 0b");
  Serial.println(val, BIN);
  delay(200);
}

How to test in the playground

  1. Drag a DIP Switch 8 onto the canvas.
  2. Wire B pins (1B–8B) to Arduino pins 2–9.
  3. Wire all A pins (1A–8A) together to GND.
  4. Paste the sketch, open the Serial Monitor, and toggle individual switches.
⌨️

Membrane Keypad

wokwi-membrane-keypad

A 4Γ—4 membrane keypad with 16 keys (0–9, A–D, *, #). Uses 4 row + 4 column wires (8 total).

Wiring

Component pinArduino pin
R1Pin 9
R2Pin 8
R3Pin 7
R4Pin 6
C1Pin 5
C2Pin 4
C3Pin 3
C4Pin 2

Sketch

// Membrane Keypad β€” print key to Serial Monitor
#include <Keypad.h>

const byte ROWS = 4, COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
  Serial.begin(9600);
}

void loop() {
  char key = keypad.getKey();
  if (key) {
    Serial.print("Key: ");
    Serial.println(key);
  }
}

How to test in the playground

  1. Drag a Membrane Keypad onto the canvas.
  2. Wire R1–R4 to pins 9, 8, 7, 6 and C1–C4 to pins 5, 4, 3, 2.
  3. Paste the sketch (requires the Keypad library) and click Run.
  4. Open the Serial Monitor and press keys β€” each press prints the character.

Potentiometers

🎚️

Potentiometer

wokwi-potentiometer

A 10 kΞ© rotary potentiometer. Rotate it to output an analog voltage on SIG (0–5V).

Wiring

Component pinArduino pin
GNDGND
SIG (wiper)A0
VCC5V

Sketch

// Potentiometer β€” read value and dim LED via PWM
const int POT = A0;
const int LED = 9;  // PWM pin

void setup() {
  Serial.begin(9600);
}

void loop() {
  int raw = analogRead(POT);          // 0 – 1023
  int brightness = raw / 4;           // 0 – 255 for analogWrite
  analogWrite(LED, brightness);
  Serial.print("Pot: ");
  Serial.print(raw);
  Serial.print("  Brightness: ");
  Serial.println(brightness);
  delay(50);
}

How to test in the playground

  1. Drag a Potentiometer and an LED (with 220 Ξ© resistor) onto the canvas.
  2. Wire: GND β†’ GND, VCC β†’ 5V, SIG β†’ A0.
  3. Wire LED anode β†’ Pin 9, cathode β†’ GND.
  4. Paste the sketch and click Run β€” rotate the potentiometer to dim the LED.
🎚️

Slide Potentiometer

wokwi-slide-potentiometer

A linear slide potentiometer β€” functionally identical to the rotary version but with a sliding thumb.

Wiring

Component pinArduino pin
VCC5V
SIG (wiper)A0
GNDGND

Sketch

// Slide Potentiometer β€” map position to servo angle
#include <Servo.h>

const int POT = A0;
Servo servo;

void setup() {
  servo.attach(9);
}

void loop() {
  int raw   = analogRead(POT);           // 0 – 1023
  int angle = map(raw, 0, 1023, 0, 180);
  servo.write(angle);
  delay(15);
}

How to test in the playground

  1. Drag a Slide Potentiometer and a Servo Motor onto the canvas.
  2. Wire: VCC β†’ 5V, SIG β†’ A0, GND β†’ GND.
  3. Wire Servo: PWM β†’ Pin 9, V+ β†’ 5V, GND β†’ GND.
  4. Paste the sketch and click Run β€” slide the thumb to move the servo.

Sensors

🌑️

DHT22

wokwi-dht22

A digital temperature and humidity sensor. Single-wire protocol β€” one data pin is all you need.

Wiring

Component pinArduino pin
VCC5V
SDA (Data)Pin 2 (via 10 kΞ© pull-up to 5V)
GNDGND

Sketch

// DHT22 β€” print temperature and humidity every 2 s
#include <DHT.h>

#define DHTPIN  2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Sensor error!");
  } else {
    Serial.print("Temp: ");
    Serial.print(t);
    Serial.print(" Β°C   Humidity: ");
    Serial.print(h);
    Serial.println(" %");
  }
  delay(2000);
}

How to test in the playground

  1. Drag a DHT22 onto the canvas.
  2. Wire VCC β†’ 5V, GND β†’ GND, SDA β†’ Pin 2.
  3. Add a 10 kΞ© pull-up resistor between SDA and 5V.
  4. Paste the sketch (requires DHT library) and open the Serial Monitor.

Note: The DHT22 needs at least 2 s between readings. Use dht.readTemperatureF() for Fahrenheit.

🌑️

NTC Temp Sensor

wokwi-ntc-temperature-sensor

An NTC thermistor breakout with a built-in voltage divider. Outputs an analog voltage proportional to temperature.

Wiring

Component pinArduino pin
GNDGND
VCC5V
OUT (Analog)A0

Sketch

// NTC Temp Sensor β€” print temperature in Β°C
const int NTC = A0;
const float SERIES_R = 10000.0;   // 10 kΞ© series resistor
const float NOMINAL_R = 10000.0;  // 10 kΞ© at 25 Β°C
const float NOMINAL_T = 25.0;     // Β°C
const float B_COEFF   = 3950.0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  float raw = analogRead(NTC);
  float r = SERIES_R * (1023.0 / raw - 1.0);
  float steinhart = log(r / NOMINAL_R) / B_COEFF;
  steinhart += 1.0 / (NOMINAL_T + 273.15);
  float tempC = (1.0 / steinhart) - 273.15;

  Serial.print("Temp: ");
  Serial.print(tempC, 1);
  Serial.println(" Β°C");
  delay(1000);
}

How to test in the playground

  1. Drag an NTC Temp Sensor onto the canvas.
  2. Wire GND β†’ GND, VCC β†’ 5V, OUT β†’ A0.
  3. Paste the sketch and open the Serial Monitor.
  4. Click the component in the simulation to adjust the simulated temperature.
πŸ’‘

Photoresistor

wokwi-photoresistor-sensor

An LDR (light-dependent resistor) breakout with both analog and digital outputs.

Wiring

Component pinArduino pin
VCC5V
GNDGND
AO (Analog Out)A0
DO (Digital Out)Pin 2 (optional threshold output)

Sketch

// Photoresistor β€” react to light level
const int LDR_A = A0;
const int LDR_D = 2;
const int LED   = 13;

void setup() {
  pinMode(LDR_D, INPUT);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int light = analogRead(LDR_A);         // 0 (dark) – 1023 (bright)
  bool dark  = digitalRead(LDR_D) == LOW; // LOW when below threshold pot

  digitalWrite(LED, dark);               // LED on in the dark

  Serial.print("Light: ");
  Serial.print(light);
  Serial.print("  Dark: ");
  Serial.println(dark ? "yes" : "no");
  delay(200);
}

How to test in the playground

  1. Drag a Photoresistor onto the canvas.
  2. Wire VCC β†’ 5V, GND β†’ GND, AO β†’ A0.
  3. Optionally wire DO β†’ Pin 2 for threshold detection.
  4. Paste the sketch β€” in the simulation you can click and drag the light slider on the component.
πŸ‘οΈ

PIR Motion Sensor

wokwi-pir-motion-sensor

A passive infrared (PIR) sensor that outputs HIGH for ~2 s whenever it detects movement.

Wiring

Component pinArduino pin
VCC5V
OUTPin 2
GNDGND

Sketch

// PIR Motion Sensor β€” trigger an LED on motion
const int PIR = 2;
const int LED = 13;

void setup() {
  pinMode(PIR, INPUT);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("Waiting for motion...");
}

void loop() {
  if (digitalRead(PIR) == HIGH) {
    digitalWrite(LED, HIGH);
    Serial.println("Motion detected!");
    delay(2000);             // keep LED on for 2 s
  } else {
    digitalWrite(LED, LOW);
  }
}

How to test in the playground

  1. Drag a PIR Motion Sensor onto the canvas.
  2. Wire VCC β†’ 5V, GND β†’ GND, OUT β†’ Pin 2.
  3. Wire LED (with resistor) to Pin 13.
  4. Paste the sketch and click Run β€” in the simulation, click the sensor to trigger motion.

Note: The PIR output stays HIGH for about 2 s after the last trigger.

πŸ“‘

HC-SR04 Ultrasonic

wokwi-hc-sr04

An ultrasonic distance sensor (2 cm – 4 m range). Send a 10 Β΅s trigger pulse and measure the echo duration.

Wiring

Component pinArduino pin
VCC5V
TRIGPin 9
ECHOPin 10
GNDGND

Sketch

// HC-SR04 β€” measure distance and print to Serial Monitor
const int TRIG = 9;
const int ECHO = 10;

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  Serial.begin(9600);
}

float getDistanceCm() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);

  long duration = pulseIn(ECHO, HIGH);
  return duration * 0.034 / 2.0;  // cm
}

void loop() {
  float dist = getDistanceCm();
  Serial.print("Distance: ");
  Serial.print(dist, 1);
  Serial.println(" cm");
  delay(200);
}

How to test in the playground

  1. Drag an HC-SR04 onto the canvas.
  2. Wire VCC β†’ 5V, GND β†’ GND, TRIG β†’ Pin 9, ECHO β†’ Pin 10.
  3. Paste the sketch and open the Serial Monitor.
  4. In the simulation, click and drag the obstacle slider on the sensor to change the distance.

Note: The echo pin outputs 5V β€” safe to connect directly on a 5V Arduino Uno.

πŸ’¨

Gas Sensor

wokwi-gas-sensor

An MQ-type gas sensor with analog and digital outputs. Detects smoke, LPG, CO, and other gases.

Wiring

Component pinArduino pin
VCC5V
GNDGND
AOUT (Analog)A0
DOUT (Digital)Pin 2 (threshold alarm)

Sketch

// Gas Sensor β€” read analog level and trigger alarm
const int GAS_A = A0;
const int GAS_D = 2;
const int BUZZER = 8;

void setup() {
  pinMode(GAS_D, INPUT);
  pinMode(BUZZER, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int level = analogRead(GAS_A);
  bool alarm = digitalRead(GAS_D) == HIGH;  // HIGH when gas exceeds threshold

  digitalWrite(BUZZER, alarm);

  Serial.print("Gas level: ");
  Serial.print(level);
  Serial.print(alarm ? "  *** ALARM ***" : "  OK");
  Serial.println();
  delay(200);
}

How to test in the playground

  1. Drag a Gas Sensor and a Buzzer onto the canvas.
  2. Wire VCC β†’ 5V, GND β†’ GND, AOUT β†’ A0, DOUT β†’ Pin 2.
  3. Wire Buzzer + β†’ Pin 8, Buzzer βˆ’ β†’ GND.
  4. Paste the sketch and open the Serial Monitor β€” in the sim, drag the gas slider on the sensor.

Note: The digital threshold is set by the small potentiometer on the physical module. In simulation it behaves as mid-range.

πŸ•ΉοΈ

Analog Joystick

wokwi-analog-joystick

A 2-axis analog joystick with a push button. X and Y output 0–5V (centre β‰ˆ 2.5V).

Wiring

Component pinArduino pin
VCC5V
VERT (Y axis)A1
HORZ (X axis)A0
SEL (Button)Pin 2
GNDGND

Sketch

// Analog Joystick β€” print X, Y and button state
const int JOY_X = A0;
const int JOY_Y = A1;
const int JOY_BTN = 2;

void setup() {
  pinMode(JOY_BTN, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  int x   = analogRead(JOY_X);
  int y   = analogRead(JOY_Y);
  bool btn = digitalRead(JOY_BTN) == LOW;

  Serial.print("X: "); Serial.print(x);
  Serial.print("  Y: "); Serial.print(y);
  Serial.print("  Btn: "); Serial.println(btn ? "pressed" : "released");
  delay(100);
}

How to test in the playground

  1. Drag an Analog Joystick onto the canvas.
  2. Wire VCC β†’ 5V, GND β†’ GND, HORZ β†’ A0, VERT β†’ A1, SEL β†’ Pin 2.
  3. Paste the sketch and open the Serial Monitor.
  4. In the simulation, drag the joystick thumb or click it to press the button.

Note: Centre position reads approximately 512 on each axis. Dead-zone the input if Β±20 counts of noise causes issues.

🌑️

MPU-6050 IMU

wokwi-mpu6050

A 6-axis IMU (3-axis accelerometer + 3-axis gyroscope) over IΒ²C. Great for orientation and motion sensing.

Wiring

Component pinArduino pin
VCC3.3V
GNDGND
SDAA4 (SDA)
SCLA5 (SCL)

Sketch

// MPU-6050 β€” print accelerometer values over I2C
#include <Wire.h>
#include <MPU6050_light.h>

MPU6050 mpu(Wire);

void setup() {
  Wire.begin();
  Serial.begin(9600);
  byte status = mpu.begin();
  if (status != 0) {
    Serial.print("MPU init failed: "); Serial.println(status);
    while (true);
  }
  Serial.println("Calibrating...");
  mpu.calcOffsets();
  Serial.println("Ready!");
}

void loop() {
  mpu.update();
  Serial.print("Ax: "); Serial.print(mpu.getAccX(), 2);
  Serial.print("  Ay: "); Serial.print(mpu.getAccY(), 2);
  Serial.print("  Az: "); Serial.println(mpu.getAccZ(), 2);
  delay(200);
}

How to test in the playground

  1. Drag an MPU-6050 onto the canvas.
  2. Wire VCC β†’ 3.3V, GND β†’ GND, SDA β†’ A4, SCL β†’ A5.
  3. Paste the sketch (requires MPU6050_light library) and open the Serial Monitor.
  4. In the simulation, click the component and adjust the orientation sliders.

Note: AD0 tied to GND = IΒ²C address 0x68. Tie AD0 to 3.3V for 0x69 (useful when two MPUs are on the same bus).

⏰

DS1307 RTC

wokwi-ds1307

A battery-backed real-time clock (RTC) chip over IΒ²C. Keeps time even when the Arduino is powered off.

Wiring

Component pinArduino pin
GNDGND
5V5V
SDAA4 (SDA)
SCLA5 (SCL)

Sketch

// DS1307 RTC β€” set time once then read it every second
#include <Wire.h>
#include <RTClib.h>

RTC_DS1307 rtc;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  if (!rtc.begin()) {
    Serial.println("RTC not found!");
    while (true);
  }
  if (!rtc.isrunning()) {
    // Set to compile-time timestamp on first run
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void loop() {
  DateTime now = rtc.now();
  Serial.print(now.year()); Serial.print('-');
  Serial.print(now.month()); Serial.print('-');
  Serial.print(now.day()); Serial.print(' ');
  Serial.print(now.hour()); Serial.print(':');
  Serial.print(now.minute()); Serial.print(':');
  Serial.println(now.second());
  delay(1000);
}

How to test in the playground

  1. Drag a DS1307 RTC onto the canvas.
  2. Wire GND β†’ GND, 5V β†’ 5V, SDA β†’ A4, SCL β†’ A5.
  3. Paste the sketch (requires RTClib) and click Run.
  4. The first run sets the time to the compile timestamp β€” subsequent runs read it back.
πŸ“‘

IR Receiver

wokwi-ir-receiver

An infrared receiver that decodes 38 kHz IR signals from a remote control.

Wiring

Component pinArduino pin
GNDGND
VCC5V
DAT (Data)Pin 11

Sketch

// IR Receiver β€” print received codes to Serial Monitor
#include <IRremote.hpp>

const int IR_PIN = 11;
IRrecv irrecv(IR_PIN);

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();
  Serial.println("Point the remote at the receiver and press a key.");
}

void loop() {
  if (irrecv.decode()) {
    Serial.print("Protocol: ");
    Serial.print(irrecv.decodedIRData.protocol);
    Serial.print("  Code: 0x");
    Serial.println(irrecv.decodedIRData.decodedRawData, HEX);
    irrecv.resume();  // ready for next signal
  }
}

How to test in the playground

  1. Drag an IR Receiver and an IR Remote onto the canvas.
  2. Wire IR Receiver: GND β†’ GND, VCC β†’ 5V, DAT β†’ Pin 11.
  3. Paste the sketch (requires IRremote library) and click Run.
  4. Click buttons on the IR Remote in the simulation β€” codes appear in the Serial Monitor.

Note: Use the printed hex codes to map remote buttons to actions in your project.

πŸ“»

IR Remote

wokwi-ir-remote

An IR remote control (NEC protocol). Use it with the IR Receiver component β€” it has no wiring of its own.

Sketch

// IR Remote β€” no wiring needed on the remote itself.
// Wire the IR Receiver and use this sketch to read remote codes.
// See the IR Receiver guide for the full sketch.

How to test in the playground

  1. The IR Remote requires no wiring β€” it's a simulation-only input device.
  2. Drag it onto the canvas alongside an IR Receiver.
  3. Run the IR Receiver sketch and click buttons on the remote to see decoded values.
πŸ”„

KY-040 Encoder

wokwi-ky-040

A KY-040 incremental rotary encoder with a push button. Turning it generates quadrature pulses on CLK and DT.

Wiring

Component pinArduino pin
CLKPin 2 (interrupt)
DTPin 3
SW (Button)Pin 4
VCC (+)5V
GNDGND

Sketch

// KY-040 Encoder β€” track rotation position and button press
const int CLK = 2;
const int DT  = 3;
const int SW  = 4;

volatile int position = 0;
int lastCLK;

void onCLK() {
  int clk = digitalRead(CLK);
  int dt  = digitalRead(DT);
  if (clk != lastCLK) {
    position += (dt != clk) ? 1 : -1;
    lastCLK = clk;
  }
}

void setup() {
  pinMode(CLK, INPUT);
  pinMode(DT, INPUT);
  pinMode(SW, INPUT_PULLUP);
  lastCLK = digitalRead(CLK);
  attachInterrupt(digitalPinToInterrupt(CLK), onCLK, CHANGE);
  Serial.begin(9600);
}

void loop() {
  static int lastPos = 0;
  if (position != lastPos) {
    Serial.print("Position: ");
    Serial.println(position);
    lastPos = position;
  }
  if (digitalRead(SW) == LOW) {
    Serial.println("Button pressed!");
    delay(200);
  }
}

How to test in the playground

  1. Drag a KY-040 Encoder onto the canvas.
  2. Wire CLK β†’ Pin 2, DT β†’ Pin 3, SW β†’ Pin 4, VCC β†’ 5V, GND β†’ GND.
  3. Paste the sketch and open the Serial Monitor.
  4. In the simulation, click the encoder to rotate it or press its button.

Note: CLK is wired to an interrupt pin (2 or 3 on Uno) for reliable high-speed decoding.

Actuators & Drivers

πŸ””

Buzzer

wokwi-buzzer

A passive piezo buzzer. Drive it with tone() to play notes at any frequency.

Wiring

Component pinArduino pin
Positive (+)Pin 8
Negative (βˆ’)GND

Sketch

// Buzzer β€” plays a simple melody
const int BUZZER = 8;

// Frequencies for C4, E4, G4, C5
const int notes[] = {262, 330, 392, 523};
const int durations[] = {300, 300, 300, 600};

void setup() {
  pinMode(BUZZER, OUTPUT);
}

void loop() {
  for (int i = 0; i < 4; i++) {
    tone(BUZZER, notes[i], durations[i]);
    delay(durations[i] + 50);  // slight gap between notes
  }
  noTone(BUZZER);
  delay(800);
}

How to test in the playground

  1. Drag a Buzzer onto the canvas.
  2. Wire + β†’ Pin 8, βˆ’ β†’ GND.
  3. Paste the sketch and click Run β€” the buzzer plays a C major arpeggio on repeat.

Note: Use tone(pin, freq) to start and noTone(pin) to stop. The Uno uses Timer2 for tone β€” this conflicts with pins 3 and 11 PWM.

βš™οΈ

Servo Motor

wokwi-servo

A standard hobby servo motor. Control position (0°–180Β°) or continuous rotation with the Servo library.

Wiring

Component pinArduino pin
Signal (orange/yellow)Pin 9 (PWM)
Power (red)5V
Ground (brown/black)GND

Sketch

// Servo Sweep β€” moves servo from 0Β° to 180Β° and back
#include <Servo.h>

Servo myServo;

void setup() {
  myServo.attach(9);  // Servo signal wire β†’ pin 9
  Serial.begin(9600);
  Serial.println("Servo ready!");
}

void loop() {
  // Sweep from 0Β° to 180Β°
  for (int angle = 0; angle <= 180; angle++) {
    myServo.write(angle);
    delay(15);  // ~15ms per degree = ~2.7s full sweep
  }

  // Sweep back from 180Β° to 0Β°
  for (int angle = 180; angle >= 0; angle--) {
    myServo.write(angle);
    delay(15);
  }
}

How to test in the playground

  1. Drag a Servo Motor onto the canvas.
  2. Wire Signal (orange/yellow) β†’ Pin 9, Power (red) β†’ 5V, Ground (brown/black) β†’ GND.
  3. Paste the sketch and click Run β€” the servo sweeps back and forth.

Note: Pin 9 uses Timer1 for PWM, which is properly initialised for servo control.

πŸ”§

Stepper Motor

wokwi-stepper-motor

A bipolar stepper motor. Drive it with an A4988 or similar driver board for precise position control.

Wiring

Component pinArduino pin
A+ / A-A4988 OUT1A / OUT1B
B+ / B-A4988 OUT2A / OUT2B

Sketch

// Stepper Motor β€” rotate 360Β° CW then 360Β° CCW (using AccelStepper)
#include <AccelStepper.h>

// A4988 driver: STEP β†’ Pin 3, DIR β†’ Pin 4
AccelStepper stepper(AccelStepper::DRIVER, 3, 4);

const int STEPS_PER_REV = 200;

void setup() {
  stepper.setMaxSpeed(400);
  stepper.setAcceleration(200);
  stepper.moveTo(STEPS_PER_REV);  // one full CW rotation
}

void loop() {
  if (stepper.distanceToGo() == 0) {
    stepper.moveTo(-stepper.currentPosition());  // reverse
  }
  stepper.run();
}

How to test in the playground

  1. Drag a Stepper Motor onto the canvas. You also need an A4988 driver (add as a custom element if not in palette).
  2. Wire A4988 outputs to the stepper coils: OUT1A/1B β†’ A+/Aβˆ’, OUT2A/2B β†’ B+/Bβˆ’.
  3. Wire Arduino Pin 3 β†’ A4988 STEP, Pin 4 β†’ A4988 DIR.
  4. Supply 8–35V to the driver VMOT and logic 5V to VDD.
  5. Paste the sketch (requires AccelStepper library) and click Run.

Note: 200 steps/revolution = 1.8Β° per step (standard). Use microstepping on the A4988 for smoother motion.

βš™οΈ

Biaxial Stepper

wokwi-biaxial-stepper

Two coaxial stepper motors sharing a shaft β€” used for pen plotters, robots, and pan-tilt mounts.

Wiring

Component pinArduino pin
A1+/A1βˆ’/B1+/B1βˆ’Driver 1 coil outputs
A2+/A2βˆ’/B2+/B2βˆ’Driver 2 coil outputs

Sketch

// Biaxial Stepper β€” run both axes simultaneously
#include <AccelStepper.h>

AccelStepper axis1(AccelStepper::DRIVER, 3, 4);  // STEP=3, DIR=4
AccelStepper axis2(AccelStepper::DRIVER, 5, 6);  // STEP=5, DIR=6

const int STEPS = 200;

void setup() {
  axis1.setMaxSpeed(300); axis1.setAcceleration(150);
  axis2.setMaxSpeed(300); axis2.setAcceleration(150);
  axis1.moveTo(STEPS);
  axis2.moveTo(-STEPS);  // opposite direction
}

void loop() {
  if (axis1.distanceToGo() == 0) axis1.moveTo(-axis1.currentPosition());
  if (axis2.distanceToGo() == 0) axis2.moveTo(-axis2.currentPosition());
  axis1.run();
  axis2.run();
}

How to test in the playground

  1. Drag a Biaxial Stepper onto the canvas β€” wire each axis to its own A4988 driver.
  2. Axis 1 driver: STEP β†’ Pin 3, DIR β†’ Pin 4.
  3. Axis 2 driver: STEP β†’ Pin 5, DIR β†’ Pin 6.
  4. Paste the sketch (requires AccelStepper) and click Run β€” both axes rotate in opposite directions.
⚑

KS2E Relay

wokwi-ks2e-m-dc5

A 5V DPDT relay. Use it to switch mains or high-current loads that the Arduino cannot drive directly.

Wiring

Component pinArduino pin
COIL1Pin 7 (via transistor/ULN2003)
COIL2 (GND)GND
P1 (COM1)Load positive
NO1Power supply positive

Sketch

// KS2E Relay β€” click a relay on and off every 2 s
// Wire the coil through a transistor (e.g. 2N2222) or ULN2003
const int RELAY = 7;

void setup() {
  pinMode(RELAY, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(RELAY, HIGH);  // coil energised β†’ NO closes
  Serial.println("Relay ON");
  delay(2000);

  digitalWrite(RELAY, LOW);   // coil off β†’ NO opens
  Serial.println("Relay OFF");
  delay(2000);
}

How to test in the playground

  1. Drag a KS2E Relay onto the canvas.
  2. Drive COIL1 from Arduino Pin 7 via a NPN transistor (base resistor ~1 kΞ©) and a flyback diode.
  3. Wire COIL2 to GND, collector to COIL1, emitter to GND.
  4. Wire your load between NO1 and the supply, with P1 (COM1) as the switching point.
  5. Paste the sketch and click Run β€” the relay clicks on and off every 2 s.

Note: Never drive the relay coil directly from an Arduino pin β€” use a transistor or a ULN2003 driver IC.

Communication & Storage

πŸ’Ύ

MicroSD Card

wokwi-microsd-card

A MicroSD card breakout over SPI. Read and write FAT16/FAT32 files with the built-in SD library.

Wiring

Component pinArduino pin
VCC5V
GNDGND
CSPin 10
DI (MOSI)Pin 11 (SPI MOSI)
SCKPin 13 (SPI SCK)
DO (MISO)Pin 12 (SPI MISO)

Sketch

// MicroSD Card β€” log sensor data to a CSV file
#include <SD.h>

const int CS_PIN = 10;
int counter = 0;

void setup() {
  Serial.begin(9600);
  if (!SD.begin(CS_PIN)) {
    Serial.println("SD init failed!");
    return;
  }
  Serial.println("SD card ready.");
  File f = SD.open("data.csv", FILE_WRITE);
  if (f) { f.println("index,value"); f.close(); }
}

void loop() {
  int val = analogRead(A0);
  File f = SD.open("data.csv", FILE_WRITE);
  if (f) {
    f.print(counter++);
    f.print(',');
    f.println(val);
    f.close();
    Serial.print("Logged: "); Serial.println(val);
  }
  delay(1000);
}

How to test in the playground

  1. Drag a MicroSD Card onto the canvas.
  2. Wire VCC β†’ 5V, GND β†’ GND, CS β†’ Pin 10, MOSI β†’ Pin 11, SCK β†’ Pin 13, MISO β†’ Pin 12.
  3. Paste the sketch and click Run β€” the sketch creates data.csv and appends a new reading every second.
  4. Open the Serial Monitor to confirm writes succeed.

Note: Format the SD card as FAT32. The SD library expects FAT16 or FAT32.

βš–οΈ

HX711 Load Cell

wokwi-hx711

A 24-bit ADC designed for bridge sensors (load cells / strain gauges). Communicates via a simple two-wire protocol.

Wiring

Component pinArduino pin
VCC5V
GNDGND
DT (Data)Pin 3
SCK (Clock)Pin 2

Sketch

// HX711 Load Cell β€” print raw and calibrated weight
#include <HX711.h>

const int DOUT = 3;
const int CLK  = 2;

HX711 scale;
const float CALIBRATION = 420.0;  // adjust after calibration

void setup() {
  Serial.begin(9600);
  scale.begin(DOUT, CLK);
  scale.set_scale(CALIBRATION);
  scale.tare();  // reset to zero
  Serial.println("Place weight on scale...");
}

void loop() {
  if (scale.is_ready()) {
    float grams = scale.get_units(5);  // average of 5 readings
    Serial.print("Weight: ");
    Serial.print(grams, 1);
    Serial.println(" g");
  }
  delay(500);
}

How to test in the playground

  1. Drag an HX711 onto the canvas.
  2. Wire VCC β†’ 5V, GND β†’ GND, DT β†’ Pin 3, SCK β†’ Pin 2.
  3. Connect your load cell's four wires to the HX711's E+/Eβˆ’/A+/Aβˆ’ terminals.
  4. Paste the sketch (requires HX711 library), click Run, and open the Serial Monitor.
  5. In the simulation, adjust the force slider on the component to simulate weight.

Note: The calibration factor (420.0) must be tuned to your specific load cell β€” weigh a known mass and adjust.