R2D2 Creation with Codey π€ Ep.4
Source: Dev.to
Episode 4: The All-Seeing Eye
βImpressive. Most Impressive.β ποΈ
Darth Vader enters β wait, no. Wrong side.
Luke Skywalker stands at the workbench, holding an HC-SR04 ultrasonic sensor up to the light.
LUKE: βIβve always wondered how he knew. How R2 always knew when something was coming. When an obstacle was in the way. When the Death Star trench was approaching. He sees things. Feels them. Before anyone else does.β
R2-D2 beeps softly, as if this is a deeply personal topic.
LUKE: βToday we give him that. The sensor eye. And this timeββ he gestures at the screen ββCodeyβs going to show us something called Auto Error Fixing. Because I have a feeling weβre going to need it.β
From somewhere unseen, a voice floats through the Force.
VADER: βYour lack of a level-shifted data pinβ¦ disturbs me.β
LUKE: βWeβre on 5V. Weβll be fine.β
ποΈ SIPOC β The Sensor Eye System
Suppliers Inputs Process Outputs Customers
You (the maker) βAdd HC-SR04 on pins 9/10 and PIR on pin 2, react with beeps and dome color changesβ Codey writes sensor code, integrates with sound and dome systems from previous episodes Unified firmware: sense β respond with light + sound R2-D2 β who now reacts to the world around him
HC-SR04 Sensor 5V power, TRIG pulse from Arduino Emits ultrasonic pulse, measures return echo time Distance in centimetres The code β which decides how close is βtoo closeβ
PIR Motion Sensor 5V power, GPIO input to Arduino Digital HIGH when motion detected A HIGH signal on the interrupt pin The code β which triggers the alert sequence
Auto Error Fixing A compilation error from incorrect code Codey reads the error log, rewrites the offending lines, recompiles Fixed code that compiles successfully You β who get working firmware without decoding C++ error messages
Vision Feature A photo of your breadboard Codey reads the image and identifies component placement A verification report: βWiring looks correctβ or specific mismatches You β who confirm wiring before upload
The Components π§
C-3PO enters with a tray of precisely arranged components.
C-3PO: βToday we add perception to R2-D2βs capabilities. The HC-SR04 ultrasonic sensor β which I should note operates at 5V and is therefore perfectly compatible with the Arduino UNO without any level shifting, which is a relief β and the HC-SR501 PIR motion sensor, also 5V compatible. I have pre-sorted them by height to prevent any confusion.β
R2-D2 beeps something that sounds like βnobody asked you to sort them.β
Component Quantity Notes
Arduino UNO R3 1 Our trusted base
HC-SR04 ultrasonic sensor 1 5V, TRIG + ECHO pins
HC-SR501 PIR sensor 1 5V, single digital output
10kΞ© resistor 1 PIR output pull-down (some modules need it)
Jumper wires 6
USB cable 1
Plus the buzzer from Episode 2 and NeoPixel ring from Episode 3 β we are building on the complete system.
The Codey Prompt β Building on What We Have π¬
Han Solo points at the screen.
HAN: βHereβs the thing about AI coding tools β you donβt start from scratch every time. You tell it what already exists and what you want to add. Context is everything. Same in the smuggling business.β
LUKE: βYouβre comparing Codey to smuggling?β
HAN: βIβm saying context matters. Do you want the lecture or the sensor?β
Open your Codey project (from Episode 3 milestone). In Agent mode:
We're continuing the R2-D2 build from the previous episodes.
Existing systems:
- Piezo buzzer on pin 8 (tone-based sounds)
- NeoPixel ring (12 LEDs) on pin 6 (dome animations)
Add these sensor systems:
1. HC-SR04 ultrasonic sensor
- TRIG on pin 9, ECHO on pin 10
- Measure distance every 150ms (non-blocking)
- If distance
#include "animations.h"
void setup() {
Serial.begin(9600);
initAnimations();
// Sensor pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(PIR_PIN, INPUT);
// PIR interrupt β fires on rising edge (motion start)
attachInterrupt(digitalPinToInterrupt(PIR_PIN), onMotion, RISING);
Serial.println("R2-D2 sensor eye online!");
}
void loop() {
updateAnimationsSensors(); // new unified call
}
Enter fullscreen mode
Exit fullscreen mode
Auto Error Fixing in Action π οΈ
And then it happens.
LUKE: βI tried to compile andββ
Compilation Error:
animations.h:47: error: 'IRAM_ATTR' was not declared in this scope
animations.h:47: note: 'IRAM_ATTR' is an ESP32 attribute, not available on AVR
Enter fullscreen mode
Exit fullscreen mode
LUKE: βWhat does that mean? I justββ
Before Luke can panic, the screen updates automatically.
CODEY AUTO ERROR FIX:
I see the error! IRAM_ATTR is an ESP32-specific macro for placing
interrupt handlers in IRAM (fast memory). The Arduino UNO uses
an ATmega328P β it doesn't have IRAM at all.
For Arduino UNO, the interrupt handler just needs the ISR() attribute,
which I used in the function signature. I'll remove IRAM_ATTR.
Fixed:
Before: void IRAM_ATTR onMotion() {
After: void onMotion() {
Recompiling...
Enter fullscreen mode
Exit fullscreen mode
β Compilation successful (after auto-fix)
Board: Arduino UNO R3
Sketch: r2d2-dome.ino + animations.h
Size: 9,104 bytes (28% of flash)
Enter fullscreen mode
Exit fullscreen mode
LUKE: βItβ¦ fixed it. By itself.β
HAN: βNever tell me the odds.β
LUKE: βThatβs not the sameββ
HAN: βThe AI fixed its own mistake without you having to understand why. Thatβs what matters.β
The Wiring Diagram β Three Systems Now π§
Click Wiring Diagram. The diagram now shows all three systems:
R2-D2 Sensor Eye + Dome + Sound β Wiring Diagram
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
[Arduino UNO R3]
POWER RAIL:
5V ββββββββββ¬ββ NeoPixel 5V
βββ HC-SR04 VCC
βββ PIR Sensor VCC
βββ (100Β΅F cap +)
GND ββββββββββ¬ββ NeoPixel GND
βββ HC-SR04 GND
βββ PIR Sensor GND
βββ (100Β΅F cap β)
SIGNALS:
Pin 6 ββββ (470Ξ©) ββββ NeoPixel DIN
Pin 8 ββββ (100Ξ©) ββββ Piezo Buzzer +
Pin 9 βββββββββββββββ HC-SR04 TRIG
Pin 10 βββββββββββββββ HC-SR04 ECHO
Pin 2 βββββββββββββββ PIR Sensor OUT
Color code:
RED = 5V / power rail
BLACK = GND rail
GREEN = NeoPixel data (pin 6)
ORANGE = Buzzer signal (pin 8)
BLUE = HC-SR04 TRIG (pin 9)
PURPLE = HC-SR04 ECHO (pin 10)
YELLOW = PIR output (pin 2)
GREY = Through resistors
Connection Table:
ββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββ
β From β To β
ββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββ€
β Arduino 5V β NeoPixel Ring: 5V β
β Arduino 5V β HC-SR04: VCC β
β Arduino 5V β PIR HC-SR501: VCC β
β Arduino 5V β 100Β΅F Cap: + (long leg) β
β Arduino GND β NeoPixel Ring: GND β
β Arduino GND β HC-SR04: GND β
β Arduino GND β PIR HC-SR501: GND β
β Arduino GND β 100Β΅F Cap: β (short leg) β
β Arduino Pin 6 β Resistor 470Ξ© β NeoPixel DIN β
β Arduino Pin 8 β Resistor 100Ξ© β Piezo (+) β
β Arduino Pin 9 β HC-SR04: TRIG β
β Arduino Pin 10 β HC-SR04: ECHO β
β Arduino Pin 2 β PIR HC-SR501: OUT β
ββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββ
Enter fullscreen mode
Exit fullscreen mode
C-3PO: βSeven signals on eight wires plus the power rail. The color-coded system makes it genuinely manageable. Download the PDF immediately β this is the most complex wiring diagram we have produced so far and I do not trust organic memory.β
Vision Feature: βIs My Breadboard Wired Correctly?β ποΈ
Luke takes a photo of his assembled breadboard.
LUKE: βBefore I upload β can I check this with Codey? Just to be sure?β
Click the Vision icon in Codey (Pro feature). Upload the breadboard photo.
Codey responds:
I can see your breadboard. Here's what I observe:
β HC-SR04 ultrasonic sensor connected:
- VCC to 5V rail β correct
- GND to GND rail β correct
- TRIG appears connected to the digital pin area (looks like pin 9) β correct
- ECHO appears connected to pin 10 β correct
β PIR sensor:
- VCC and GND connections look correct
- Signal wire appears to go to pin 2 β correct
β οΈ One potential issue:
I notice what looks like the NeoPixel data wire may not have the
470Ξ© resistor in line β it appears to connect directly. Check that
the resistor is seated in the breadboard between pin 6 and the DIN
input. A direct connection may cause LED flickering or data errors.
Everything else looks correctly laid out!
Enter fullscreen mode
Exit fullscreen mode
Luke examines the breadboard.
LUKE: βThe resistorβ¦ itβs in the wrong row. Itβs not actually connected to anything.β
R2-D2 beeps in the tone of βI could have told you that.β
LUKE: βVision just saved R2βs dome lights.β
Whatβs Next: The Holographic Projector π½οΈ
Obi-Wanβs presence fills the workshop like warm light.
OBI-WAN: βYou have given R2-D2 his lights. His sounds. His sense of the world around him. But the moment that first made the galaxy fall in love with this droid β do you remember it, Luke?β
LUKE: βHelp me, Obi-Wan Kenobi. Youβre my only hope.β
OBI-WAN: βIn Episode 5, we give Artoo his projector. The SSD1306 OLED display β small, crisp, I2C wired β will show R2βs status, messages, and a recreation of that famous message. Milestones and Rollback will save us if anything goes wrong.β
R2-D2 beeps the opening notes of the Princess Leia theme.
π Resources
HC-SR04 with Arduino: Search βHC-SR04 Arduino tutorialβ
PIR HC-SR501: Search βHC-SR501 Arduino interruptβ
Codey Online Vision: codey.online β Pro feature
Auto Error Fixing: built into all Codey plans
π€ R2D2 Creation with Codey β building the galaxyβs greatest droid, one episode at a time. May the Force β and the cloud compiler β be with you.