Black Pill sun-seeking pointer
Build it step by step
0 of 7 doneWire the programmer
SWDIO, SWCLK, GND and 3.3V.
Build both dividers
Photoresistor from 3.3 to the pin, 10 kilo-ohm from the pin to GND.
Mount the photoresistors
Side by side with a card between them.
Wire the servo
Signal to PA2, own 5 V supply, grounds joined.
Upload the sketch
The pointer centres.
Shine a torch from one side
The pointer turns towards it.
Check the direction
Set DIR to -1 if it turns away.
How do you build a black Pill sun-seeking pointer?
A Black Pill compares two photoresistors on either side of a small shade and turns a servo towards the brighter side. You need STM32 Black Pill (STM32F411CEU6), ST-Link V2 programmer (clone), LDR Photoresistor × 2, Resistor 10 kΩ × 2, SG90 Micro Servo and Breadboard 830 Tie Points. It takes 7 steps and about 160 minutes.
- Level
- Intermediate
- Time
- About 160 min
- Parts
- 6
Parts list
- Development BoardsBuy
- InterfacesBuy
- SensorsBuy
- PassivesBuyResistor 10 kΩ × 2
- Motors & ActuatorsBuySG90 Micro Servo × 1
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| ST-Link GND | Board GND | Ground |
| ST-Link SWDIO | Board DIO | Debug data |
| ST-Link SWCLK | Board DCLK | Debug clock |
| ST-Link 3.3V | Board 3.3 pin | Power from the programmer (do not also plug in USB) |
| Left photoresistor | Black Pill 3.3 pin to PA0 | Top of the left divider |
| Left 10 kilo-ohm resistor | PA0 to GND | Bottom of the left divider |
| Right photoresistor | Black Pill 3.3 pin to PA1 | Top of the right divider |
| Right 10 kilo-ohm resistor | PA1 to GND | Bottom of the right divider |
| Servo signal | Black Pill PA2 | Pulse output |
| Servo power (red) | Separate 5 V supply, not the board | A servo can pull hundreds of milliamps |
| Servo ground | Supply ground and Black Pill GND | Common ground |
Code
Two photoresistors with a small card between them form a simple direction sensor: when the light is straight ahead both see the same amount, and when it moves to one side the other is shaded and reads less. Each photoresistor forms a voltage divider with a 10 kilo-ohm resistor, so a brighter light gives a higher reading. The sketch subtracts the two readings and turns the servo a small step towards the brighter side, and keeps going until the difference is inside a dead band, which stops the pointer from hunting back and forth over small noise. Use it with a torch or a desk lamp. Never look at the sun directly and never put a lens or magnifier on this build. Fix the divider card firmly, and if the pointer turns away from the light instead of towards it, change DIR to minus one.
Runs on a virtual STM32 Blue Pill in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Servo.h>
const int LEFT_PIN = PA0, RIGHT_PIN = PA1, SERVO_PIN = PA2;
const int DEAD_BAND = 150; // ignore small differences so it does not hunt
const int DIR = 1; // change to -1 if it turns away from the light
Servo pointer;
float angle = 90;
int avgRead(int pin) {
long sum = 0;
for (int i = 0; i < 8; i++) sum += analogRead(pin);
return sum / 8;
}
void setup() {
analogReadResolution(12);
pointer.attach(SERVO_PIN, 700, 2300);
pointer.write(90);
delay(500);
}
void loop() {
int l = avgRead(LEFT_PIN);
int r = avgRead(RIGHT_PIN);
int diff = l - r; // brighter side reads higher
if (abs(diff) > DEAD_BAND) {
angle += (diff > 0 ? -1 : 1) * DIR * 0.5; // small steps towards the brighter side
angle = constrain(angle, 10, 170);
pointer.write((int)angle);
}
delay(20);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Light straight ahead | Pointer stays put |
| Torch on the left | Pointer turns left |
| Torch on the right | Pointer turns right |
| Covered completely | Pointer stays still |
| Small change in room light | No hunting |
If it does not work
Turns away from the light
Set DIR to -1.
Hunts back and forth
Increase DEAD_BAND.
Does not move
Check both dividers and the servo power.
Servo twitches
Use a separate 5 V supply and common ground.
Upload fails
Check the ST-Link wires and the board settings.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the black Pill sun-seeking pointer?
STM32 Black Pill (STM32F411CEU6), ST-Link V2 programmer (clone), LDR Photoresistor × 2, Resistor 10 kΩ × 2, SG90 Micro Servo and Breadboard 830 Tie Points.
How long does the black Pill sun-seeking pointer take to build?
About 160 minutes for a intermediate maker, in 7 steps.
How do I know the black Pill sun-seeking pointer works?
Light straight ahead: Pointer stays put. Torch on the left: Pointer turns left. Torch on the right: Pointer turns right.
Turns away from the light: what should I check?
Set DIR to -1.
Hunts back and forth: what should I check?
Increase DEAD_BAND.
Does not move: what should I check?
Check both dividers and the servo power.
Servo twitches: what should I check?
Use a separate 5 V supply and common ground.
