PROJECT / 01 SOLO DESIGN & BUILD
Optical fluid level sensor
A custom PCB and a motorized photodiode carriage bring optical sensing, embedded control and mechanical design into one prototype.
Results at a glance
- My role
- Solo design and build: custom two-layer PCB, embedded motion control and printed optical fixtures.
- Demonstrated
- The carriage moved down to home, scanned upward and stopped in the prototype demonstration.
- Result & limits
- A button simulated detection after optical-circuit damage. Height reporting was incomplete; measurement accuracy was not established.
THE PROTOTYPE IN MOTION / 18 SECONDS
Home.
Scan.
Stop.
Watch the carriage move down to its starting position, scan upward and come to a stop.
- 01 Move down to the home position
- 02 Reverse and scan upward
- 03 Stop at the detection input
Demonstration note: the submitted report states that a button simulated the detection signal after damage to the optical circuit.
01 / THE PROBLEM
Measure a level.
Keep the sensor dry.
The concept was to measure fluid level optically, using a moving photodiode rather than placing a sensor in the liquid.
I designed the system around a Raspberry Pi Pico, a custom two-layer PCB and a motorized detector carriage. The build combined optical sensing, analog signal conditioning, stepper control and custom printed fixtures.
02 / SYSTEM ARCHITECTURE
One moving detector.
Several engineering disciplines.
I changed the sensing approach from a fixed photodiode array to a single detector carried through a vertical scan. That moved complexity into the carriage, homing routine and coordination between motion and sensing.
The PCB brought the control and sensing electronics together with routing, a ground plane and power separation. Custom printed mounts positioned the lasers, electronics, motor and detector.
03 / CONTROL SEQUENCE
Follow the scan.
See the signals.
Select a stage to trace the paths between the RP2040, motor driver, inputs and display.
DESIGN WALKTHROUGH · NOT A LIVE MEASUREMENT
Establish the starting position
GP2 pulses the motor with GP3 LOW to move down. GP4 is polled until the homing contact reads LOW.
Raspberry Pi Pico Top view · USB at top
Motor driver
Moving down · DIR LOW
Homing contact
Polling · LOW ends home
Detection input
Not polled during home
TM1637 display
Waiting
Active paths: GP2 STEP · GP3 DIR = LOW · GP4 HOME input
Colored wires identify the firmware signals, not the physical wire colors in the build. Detection was simulated with a button. Report shows the intended display transfer, which did not correctly report height.
04 / RP2040 FIRMWARE
From GPIO
to a moving detector.
I wrote the control sequence in ARM assembly, with a small C entry point calling ProgramMain. Direct register writes control the stepper and display, while polling checks the homing and detection inputs.
The scan homes until GP4 goes LOW, raises the carriage while counting steps, then stops when GP5 goes LOW. The final routine attempts to send the raw step count to the TM1637 display.
The firmware behind the scan.
main.S · ARM assembly · 408 lines.cpu cortex-m0
.thumb
.thumb_func
.global ProgramMain
.type ProgramMain, %function
.equ SIO_BASE, 0xD0000000
.equ IO_BANK0_BASE, 0x40014000
.equ GPIO_INPUT, (SIO_BASE + 0x004)
.equ GPIO_OUT_SET, (SIO_BASE + 0x014)
.equ GPIO_OUT_CLR, (SIO_BASE + 0x018)
.equ GPIO_OE_SET, (SIO_BASE + 0x024)
.equ GPIO_OE_CLR, (SIO_BASE + 0x028)
.equ PADS_BANK0_BASE, 0x4001C000
.equ ADC_BASE, 0x4004C000
//which pin
.equ PIN_STEP, 2
.equ PIN_DIR, 3
.equ PIN_CONT, 4
.equ PIN_PHDI, 5
//GPIO MASK
.equ MASK_STEP, (1 << PIN_STEP) //steps the motor in a controlled manner
.equ MASK_DIR, (1 << PIN_DIR) //changes the direction of the stepper motor with the DIR pin
.equ MASK_CC, (1 << PIN_CONT) //checks for continuity
.equ MASK_PHDI, (1 << PIN_PHDI)
.equ STEP_DELAY, 20000
.equ THOU, 1000
.equ MASK_LED, (1 << 25)
// TM1637 PIN ASSIGNMENT
.equ PIN_CLK, 6
.equ PIN_DIO, 7
.equ MASK_CLK, (1 << PIN_CLK)
.equ MASK_DIO, (1 << PIN_DIO)
// TM1637 COMMANDS
.equ TM1637_CMD_DATA, 0x40 // write data, auto-increment
.equ TM1637_CMD_ADDR0, 0xC0 // starting at digit 0
.equ TM1637_CMD_BRIGHT, 0x8F // display ON, brightness max
//GPIO#_CTRL MASKS
.equ GPIO2_CTRL, (IO_BANK0_BASE + 0x014)
.equ GPIO3_CTRL, (IO_BANK0_BASE + 0x01C)
.equ GPIO4_CTRL, (IO_BANK0_BASE + 0x024)
.equ GPIO5_CTRL, (IO_BANK0_BASE + 0x02C)
.equ GPIO6_CTRL, (IO_BANK0_BASE + 0x034)
.equ GPIO7_CTRL, (IO_BANK0_BASE + 0x03C)
.equ GPIO25_CTRL, (IO_BANK0_BASE + 0x0CC)
//GPIO26ADC
.equ GPIO26_CTRL, (IO_BANK0_BASE + 0x0D4) @ GPIO26_CTRL
.equ PADS_GPIO26, (PADS_BANK0_BASE + 0x06C) @ PADS_BANK0 GPIO26 pad
//ADC
.equ ADC_CS, (ADC_BASE + 0x00)
.equ ADC_RESULT, (ADC_BASE + 0x04)
.equ GPIO_FUNC_NULL, 0x1F
ProgramMain:
//1) Set GPIO 2,3,4,5,6,7 function to SIO (FUNCSEL = 5)
movs r1, #5
ldr r0, =GPIO2_CTRL
str r1, [r0]
ldr r0, =GPIO3_CTRL
str r1, [r0]
ldr r0, =GPIO4_CTRL
str r1, [r0]
ldr r0, =GPIO5_CTRL
str r1, [r0]
ldr r0, =GPIO6_CTRL
str r1, [r0]
ldr r0, =GPIO7_CTRL
str r1, [r0]
ldr r4, =GPIO_OE_SET
ldr r1, =(MASK_STEP | MASK_DIR | MASK_CLK | MASK_DIO)
str r1, [r4]
ldr r4, =(GPIO_OE_CLR)
ldr r1, =(MASK_CC | MASK_PHDI)
str r1, [r4]
ldr r0, =GPIO_OUT_SET
ldr r2, =GPIO_INPUT
ldr r3, =GPIO_OUT_CLR
mov r5, #0
ldr r6, =MASK_STEP
ldr r7, =STEP_DELAY
bl dbg_led_init
b StepCal
PhotoDiodeMeas:
bl dbg_led_on
movs r4, #MASK_PHDI // make r4 the photodiode mask (overwrites the old MASK_CC)
PhotoLoop:
ldr r1, [r2] // r2 already = GPIO_INPUT from ProgramMain
tst r1, r4 // test GP5
beq LaserGone // GP5 LOW -> stop/report
bl StepUp
b PhotoLoop
LaserGone:
b report
//Testloop:
//b Testloop
dbg_led_init:
push {r0}
// Set GP25 function = SIO
ldr r0, =GPIO25_CTRL
mov r1, #5
str r1, [r0]
// Enable output on GP25
ldr r0, =GPIO_OE_SET
ldr r1, =MASK_LED
str r1, [r0]
pop {r0}
bx lr
dbg_led_on:
push {r0}
ldr r0, =GPIO_OUT_SET
ldr r1, =MASK_LED
str r1, [r0]
pop {r0}
bx lr
dbg_led_off:
push {r0}
ldr r0, =GPIO_OUT_CLR
ldr r1, =MASK_LED
str r1, [r0]
pop {r0}
bx lr
dbg_led_pulse:
push {lr}
bl dbg_led_on
bl Delay @ uses your existing delay routine
bl dbg_led_off
bl Delay
pop {pc}
CallStepUp:
bl StepUp
b PhotoDiodeMeas
StepCal:
movs r4, #MASK_CC // MASK_CC = 1<<4 = 16 fits in movs
StepCal_Loop:
ldr r1, [r2] // GPIO_INPUT
tst r1, r4 // test CC
beq StepCal_Done // CC LOW => at bottom => stop homing
bl StepDown
b StepCal_Loop // CC HIGH => not at bottom => move down
StepCal_Done:
bl Delay
bl dbg_led_pulse
bl Delay
b PhotoDiodeMeas
StepUp:
//DIR HIGH
add r5, r5, #1
ldr r1, =(MASK_DIR)
ldr r0, =GPIO_OUT_SET
str r1, [r0]
push {lr}
bl Delay
bl Delay
bl Delay
bl Delay
bl Delay
bl Delay
bl Delay
bl Step
pop {pc}
StepDown:
ldr r1, =(MASK_DIR)
str r1, [r3, #0] //GPIO_OUT_CLR to GPIO3
push {lr}
bl Step
pop {pc}
Step:
push {lr}
ldr r0, =GPIO_OUT_SET
ldr r3, =GPIO_OUT_CLR
ldr r6, =MASK_STEP
bl dbg_led_on
str r6, [r0, #0]
bl Delay
bl dbg_led_off
str r6, [r3, #0]
bl Delay
pop {pc}
Delay:
push {r1}
mov r1, r7
DelayLoop:
sub r1, r1, #1
bne DelayLoop
pop {r1}
bx lr //return
report:
// r5 = number of steps (input)
// Convert r5 into 4 digits and display via TM1637
mov r0, r5 // copy step count → r0
bl SplitDigits // returns digits in r0,r1,r2,r3
bl TM1637_Display4
b endProgLoop
SplitDigits:
mov r4, r0 // working copy
// thousands = r4 / 1000
mov r0, #0
ThousandsLoop:
ldr r7, =THOU
cmp r4, r7
blt DoneThousands
sub r4, r4, r7
add r0, r0, #1
b ThousandsLoop
DoneThousands:
// hundreds = r4 / 100
mov r1, #0
HundredsLoop:
cmp r4, #100
blt DoneHundreds
sub r4, r4, #100
add r1, r1, #1
b HundredsLoop
DoneHundreds:
// tens = r4 / 10
mov r2, #0
TensLoop:
cmp r4, #10
blt DoneTens
sub r4, r4, #10
add r2, r2, #1
b TensLoop
DoneTens:
// ones = leftover
mov r3, r4
bx lr
TM1637_Display4:
push {lr}
// 1) Send DATA command (auto-increment)
bl TM1637_Start
ldr r0, =TM1637_CMD_DATA
bl TM1637_WriteByte
bl TM1637_Stop
// 2) Send Address command
bl TM1637_Start
ldr r0, =TM1637_CMD_ADDR0
bl TM1637_WriteByte
// 3) Send 4 digits (digits in r0,r1,r2,r3)
push {r1, r2, r3} // save other digits
bl TM1637_SendDigit // send r0
pop {r1, r2, r3} // restore remaining digits
mov r0, r1
bl TM1637_SendDigit
mov r0, r2
bl TM1637_SendDigit
mov r0, r3
bl TM1637_SendDigit
bl TM1637_Stop
// 4) Set brightness
bl TM1637_Start
ldr r0, =TM1637_CMD_BRIGHT
bl TM1637_WriteByte
bl TM1637_Stop
pop {pc}
// ----- TM1637 Start -----
TM1637_Start:
// DIO high, CLK high
ldr r4, =GPIO_OUT_SET
ldr r5, =(MASK_DIO | MASK_CLK)
str r5, [r4] //(DIO and CLK HIGH)
// DIO low
ldr r4, =GPIO_OUT_CLR
ldr r5, =MASK_DIO
str r5, [r4] //(DIO LOW = start)
bx lr
// ----- TM1637 Stop -----
TM1637_Stop:
ldr r4, =GPIO_OUT_CLR
ldr r5, =MASK_CLK
str r5, [r4]
ldr r4, =GPIO_OUT_CLR
ldr r5, =MASK_DIO
str r5, [r4]
// CLK high
ldr r4, =GPIO_OUT_SET
ldr r5, =MASK_CLK
str r5, [r4]
// DIO high
ldr r4, =GPIO_OUT_SET
ldr r5, =MASK_DIO
str r5, [r4]
bx lr
// ----- Write One Byte -----
TM1637_WriteByte:
// r0 = byte to send
mov r6, #8
WriteBitLoop:
// CLK low
ldr r4, =GPIO_OUT_CLR
ldr r5, =MASK_CLK
str r5, [r4]
// output LSB to DIO
mov r7, #1
tst r0, r7
beq bit0
bit1:
ldr r4, =GPIO_OUT_SET
ldr r5, =MASK_DIO
str r5, [r4]
b nextbit
bit0:
ldr r4, =GPIO_OUT_CLR
ldr r5, =MASK_DIO
str r5, [r4]
nextbit:
// CLK high
ldr r4, =GPIO_OUT_SET
ldr r5, =MASK_CLK
str r5, [r4]
// shift
lsr r0, r0, #1
sub r6, r6, #1
bne WriteBitLoop
// ignore ACK for now (makes code simpler)
bx lr
DigitTable:
.byte 0x3F // 0
.byte 0x06 // 1
.byte 0x5B // 2
.byte 0x4F // 3
.byte 0x66 // 4
.byte 0x6D // 5
.byte 0x7D // 6
.byte 0x07 // 7
.byte 0x7F // 8
.byte 0x6F // 9
TM1637_SendDigit:
push {lr}
ldr r4, =DigitTable
ldrb r0, [r4, r0] // lookup byte for digit
bl TM1637_WriteByte
pop {pc}
endProgLoop:
wfi
b endProgLoopInside the scan loop
ldr reads the GPIO inputs; tst checks GP5 using the photodiode mask.
beq LaserGone exits the scan when GP5 is LOW and branches to the reporting routine.
Otherwise, bl StepUp advances the carriage one step, then b PhotoLoop repeats the check.
The complete original script. Scroll within the code panel to read it; focus the panel to use arrow keys or Page Down. In the demonstration, a button supplied the detection signal.
Pin assignments and signal behavior+
| GPIO | Direction | Connection | Behavior |
|---|---|---|---|
| GP2 | Output | Motor driver · STEP | Step pulses |
| GP3 | Output | Motor driver · DIR | HIGH: up · LOW: down |
| GP4 | Input | Homing contact · HOME | LOW ends the homing move |
| GP5 | Input | Detection input · DETECT | LOW stops the upward scan |
| GP6 | Output | TM1637 display · CLK | Clock driven in software |
| GP7 | Output | TM1637 display · DIO | Data driven in software |
| GP25 | Output | Onboard LED | Debug indication during stepping |
GP5 is read as a digital input. Although the source declares GP26 and ADC registers, the active routine does not sample the ADC. The TM1637 routine drives both CLK and DIO as outputs and does not handle acknowledgement.
For physical Pico header locations, see the official Raspberry Pi Pico pinout.
Source files and download+
The complete main.S is displayed above. The build selects main.c and main.S; the Pico SDK is a separate dependency.
Download original source (.zip) — optional archive containing main.c, main.S, CMakeLists.txt and a readme.
Prototype scope. The demonstration used a button to simulate detection after optical-circuit damage, and height reporting was incomplete. The source and signal map document that implementation; calibrated fluid-level measurement was not established.
05 / SCHEMATICS & PCB
Inspect the circuit.
Follow the routing.
The original schematic and board screenshots from my project submission. Select a figure to enlarge it; the full PDF preserves the surrounding design notes.
06 / MECHANICAL DESIGN
Explore the parts.
See the engineering.
Four original CAD parts from my optical fluid level sensor. Select a fixture, then rotate and zoom to inspect the supports connecting the circuit board, optical path and moving detector.
Loading interactive model…
INTERACTIVE / 3DDrag to rotate · Scroll or pinch to zoom · Arrow keys rotate the focused model
PART 01 / 04
PCB mount
Supports the custom circuit board and provides mounting points for the electronics. Inspect the raised edges and attachment features in the original part.
07 / BUILD EVIDENCE
From the board
to the assembly.
08 / RESULTS & LESSONS
A prototype with
documented limitations.
What the demonstration actually showed
Component damage disabled optical detection. A button simulated the detection signal, and the display did not correctly report height. The proposed automatic water pump was not completed.
The value of this project is in the integration: a custom board, optical sensing approach, moving detector, mechanical fixtures and low-level control sequence. The report does not establish measurement accuracy, repeatability or calibrated range.
What I would verify next+
A next validation pass would isolate the detector and amplifier, verify the input transition independently of motion, check homing and step counting, then test the display and a calibrated height conversion. These are proposed next steps, not completed results.
KEEP EXPLORING