Projects / Black Pill sun-seeking pointer

Black Pill sun-seeking pointer

Intermediate2.7 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. STM32ST

    Wire the programmer

    SWDIO, SWCLK, GND and 3.3V.

  2. STM32158 lux

    Build both dividers

    Photoresistor from 3.3 to the pin, 10 kilo-ohm from the pin to GND.

  3. STM32159 lux

    Mount the photoresistors

    Side by side with a card between them.

  4. STM3259°

    Wire the servo

    Signal to PA2, own 5 V supply, grounds joined.

  5. STM32

    Upload the sketch

    The pointer centres.

  6. STM32

    Shine a torch from one side

    The pointer turns towards it.

  7. STM32

    Check the direction

    Set DIR to -1 if it turns away.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
ST-Link GNDBoard GNDGround
ST-Link SWDIOBoard DIODebug data
ST-Link SWCLKBoard DCLKDebug clock
ST-Link 3.3VBoard 3.3 pinPower from the programmer (do not also plug in USB)
Left photoresistorBlack Pill 3.3 pin to PA0Top of the left divider
Left 10 kilo-ohm resistorPA0 to GNDBottom of the left divider
Right photoresistorBlack Pill 3.3 pin to PA1Top of the right divider
Right 10 kilo-ohm resistorPA1 to GNDBottom of the right divider
Servo signalBlack Pill PA2Pulse output
Servo power (red)Separate 5 V supply, not the boardA servo can pull hundreds of milliamps
Servo groundSupply ground and Black Pill GNDCommon 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.

CheckExpected
Light straight aheadPointer stays put
Torch on the leftPointer turns left
Torch on the rightPointer turns right
Covered completelyPointer stays still
Small change in room lightNo 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 →

Ask Lark AI

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.