Built-in project · Arduino Uno R3

Solar tracker on an Arduino Uno

Four light sensors steer a pan and tilt servo pair, an INA219 measures the panel, and the sketch alternates tracked and fixed windows, printing only measured averages. The whole build, an Arduino Uno and 18 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Advanced About 45 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $43. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Comparing light sensors to find which way to point
  • A deadband so small differences do not cause jitter
  • Driving pan and tilt with two servos
  • Measuring power with an INA219 current monitor
  • Energy as power added up over time
  • Difficulty: Advanced
  • Time: About 45 minutes
  • Board: Arduino Uno R3
  • Parts: 20

How it works

Four light sensors, each with a 10k resistor, sit in a cross: top left, top right, bottom left and bottom right. They are read on A0 to A3. A pan servo on pin 9 and a tilt servo on pin 10 turn the panel toward the brighter side.

An INA219 measures the solar panel's voltage, current and power over I2C. The sketch adds up the energy to compare two modes. It tracks the light for 20 seconds, then parks the panel at pan 90 and tilt 45 for 20 seconds, and repeats.

The green LED on pin 7 means tracking and the yellow LED on pin 6 means parked. The button on pin 2 restarts the comparison. Results print to the serial monitor.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Current monitor, INA219 pin SCLArduino Uno R3 pin SCL
Current monitor, INA219 pin SDAArduino Uno R3 pin SDA
Hobby servo (2) pin PWMArduino Uno R3 pin 10
Hobby servo (1) pin PWMArduino Uno R3 pin 9
LED, green pin AArduino Uno R3 pin 7
LED, yellow pin AArduino Uno R3 pin 6
Pushbutton pin 1aArduino Uno R3 pin 2
Light sensor (1) pin 1Arduino Uno R3 pin 5V
Light sensor (2) pin 1Arduino Uno R3 pin 5V
Light sensor (3) pin 1Arduino Uno R3 pin 5V
Light sensor (4) pin 1Arduino Uno R3 pin 5V
Hobby servo (1) pin VCCArduino Uno R3 pin 5V
Hobby servo (2) pin VCCArduino Uno R3 pin 5V
Current monitor, INA219 pin VCCArduino Uno R3 pin 5V
Resistor, 10k Ω (1) pin 2Arduino Uno R3 pin GND
Resistor, 10k Ω (2) pin 2Arduino Uno R3 pin GND
Resistor, 10k Ω (3) pin 2Arduino Uno R3 pin GND
Resistor, 10k Ω (4) pin 2Arduino Uno R3 pin GND
Hobby servo (1) pin GNDArduino Uno R3 pin GND
Hobby servo (2) pin GNDArduino Uno R3 pin GND
Current monitor, INA219 pin GNDArduino Uno R3 pin GND
Solar panel, 6 V 1 W pin -Arduino Uno R3 pin GND
Resistor, 36 Ω pin 2Arduino Uno R3 pin GND
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin GND
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin GND
Pushbutton pin 2aArduino Uno R3 pin GND
Light sensor (1) pin 2Arduino Uno R3 pin A0
Resistor, 10k Ω (1) pin 1Arduino Uno R3 pin A0
Light sensor (2) pin 2Arduino Uno R3 pin A1
Resistor, 10k Ω (2) pin 1Arduino Uno R3 pin A1
Light sensor (3) pin 2Arduino Uno R3 pin A2
Resistor, 10k Ω (3) pin 1Arduino Uno R3 pin A2
Light sensor (4) pin 2Arduino Uno R3 pin A3
Resistor, 10k Ω (4) pin 1Arduino Uno R3 pin A3
Resistor, 36 Ω pin 1Current monitor, INA219 pin VIN-
Solar panel, 6 V 1 W pin +Current monitor, INA219 pin VIN+
Resistor, 220 Ω (1) pin 2LED, green pin C
Resistor, 220 Ω (2) pin 2LED, yellow pin C

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Solar tracker: aims a panel at the light, and compares tracked with fixed.
//
// It runs an honest experiment: twenty seconds tracking, twenty seconds
// parked facing south at 45 degrees, again and again, and adds up the energy
// the INA219 measures in each mode. It prints measured numbers only.
//
// Four light sensors sit in a cross around the panel: top left, top right,
// bottom left and bottom right. When the left pair sees more light than the
// right pair, the panel is pointing too far right, so the pan servo turns it
// left, and the same goes for top and bottom with the tilt servo. When every
// sensor sees the same light, the panel is facing the sun.
//
// In Mokxi, the light is yours to set: drag each sensor's slider and watch
// the servos chase it. The light does not move as the servos turn (the
// simulator does not model where the sun is), so a tracker here keeps
// turning until it reaches its limit or you even the light out.
//
// Wiring (Arduino Uno):
//   light sensors (LDR over 10 k to ground) TL A0, TR A1, BL A2, BR A3
//   pan servo D9, tilt servo D10
//   INA219 on I2C (A4 SDA, A5 SCL): panel + to VIN+, VIN- to the 36 ohm load
//   lamps: tracking D7, fixed D6; restart the comparison with the button on D2

#include <Servo.h>
#include <Wire.h>
#include <Adafruit_INA219.h>

const uint8_t TL = A0, TR = A1, BL = A2, BR = A3;
const int DEADBAND = 20;       // ignore differences smaller than this
const unsigned long STEP_MS = 40;

Servo pan;
Servo tilt;
int panAngle = 90;
int tiltAngle = 45;
unsigned long stepAt = 0;
unsigned long printAt = 0;

Adafruit_INA219 ina;
float volts = 0, milliamps = 0, milliwatts = 0;
// The comparison: twenty seconds tracking, twenty seconds parked, again and
// again. Only measured numbers are printed: the energy the INA219 saw in each
// mode, and the time spent in it.
const uint8_t TRACK_LED = 7;
const uint8_t FIXED_LED = 6;
const uint8_t RESET_BUTTON = 2;
const unsigned long WINDOW_MS = 20000;
const int PARK_PAN = 90, PARK_TILT = 45;   // facing south at 45 degrees
bool tracking = true;
unsigned long windowAt = 0;
unsigned long sampleAt = 0;
float energy[2];          // milliwatt-hours: [0] tracked, [1] fixed
float seconds[2];
float windowEnergy = 0;
bool buttonDown = false;

void stepTracker() {
  int tl = analogRead(TL), tr = analogRead(TR), bl = analogRead(BL), br = analogRead(BR);
  int left = (tl + bl) / 2, right = (tr + br) / 2;
  int top = (tl + tr) / 2, bottom = (bl + br) / 2;
  // One degree a step is slow enough to be smooth and quick enough to keep
  // up with the real sun, which moves a quarter of a degree a minute.
  if (left - right > DEADBAND) panAngle--;
  if (right - left > DEADBAND) panAngle++;
  if (top - bottom > DEADBAND) tiltAngle++;
  if (bottom - top > DEADBAND) tiltAngle--;
  panAngle = constrain(panAngle, 0, 180);
  tiltAngle = constrain(tiltAngle, 10, 170);
}

void readPanel() {
  volts = ina.getBusVoltage_V() + ina.getShuntVoltage_mV() / 1000.0;
  milliamps = ina.getCurrent_mA();
  milliwatts = ina.getPower_mW();
}

void report() {
  Serial.print(tracking ? "TRACK" : "FIXED");
  Serial.print("  ");
  Serial.print("pan ");
  Serial.print(panAngle);
  Serial.print(" tilt ");
  Serial.print(tiltAngle);
  Serial.print("  panel ");
  Serial.print(volts, 2);
  Serial.print(" V ");
  Serial.print(milliamps, 1);
  Serial.print(" mA ");
  Serial.print(milliwatts, 0);
  Serial.print(" mW");
  Serial.println();
}

void printAverage(uint8_t mode) {
  Serial.print(mode == 0 ? "tracked " : "fixed ");
  if (seconds[mode] < 1) {
    Serial.print("not measured yet");
    return;
  }
  Serial.print("average ");
  Serial.print(energy[mode] * 3600.0 / seconds[mode], 1);
  Serial.print(" mW over ");
  Serial.print(seconds[mode], 0);
  Serial.print(" s");
}

void endWindow() {
  Serial.print("== ");
  Serial.print(tracking ? "tracked" : "fixed");
  Serial.print(" window: ");
  Serial.print(windowEnergy, 3);
  Serial.println(" mWh measured");
  Serial.print("== so far: ");
  printAverage(0);
  Serial.print(", ");
  printAverage(1);
  Serial.println();
  windowEnergy = 0;
  tracking = !tracking;
  windowAt = millis();
  digitalWrite(TRACK_LED, tracking ? HIGH : LOW);
  digitalWrite(FIXED_LED, tracking ? LOW : HIGH);
  Serial.println(tracking ? "-- tracking for 20 s" : "-- parked at pan 90, tilt 45 for 20 s");
}

void sample() {
  // Energy is power times time: add up each tenth of a second's worth.
  unsigned long now = millis();
  float hours = (now - sampleAt) / 3600000.0;
  sampleAt = now;
  uint8_t mode = tracking ? 0 : 1;
  energy[mode] += milliwatts * hours;
  seconds[mode] += hours * 3600.0;
  windowEnergy += milliwatts * hours;
}

void readButton() {
  bool down = digitalRead(RESET_BUTTON) == LOW;
  if (down && !buttonDown) {
    energy[0] = energy[1] = seconds[0] = seconds[1] = 0;
    windowEnergy = 0;
    tracking = true;
    windowAt = millis();
    digitalWrite(TRACK_LED, HIGH);
    digitalWrite(FIXED_LED, LOW);
    Serial.println("-- comparison restarted: tracking for 20 s");
  }
  buttonDown = down;
}

void setup() {
  Serial.begin(115200);
  pan.attach(9);
  tilt.attach(10);
  pan.write(panAngle);
  tilt.write(tiltAngle);
  ina.begin();
  pinMode(TRACK_LED, OUTPUT);
  pinMode(FIXED_LED, OUTPUT);
  pinMode(RESET_BUTTON, INPUT_PULLUP);
  digitalWrite(TRACK_LED, HIGH);
  sampleAt = windowAt = millis();
  Serial.println("Solar tracker ready: tracking for 20 s, then parked for 20 s.");
}

void loop() {
  unsigned long now = millis();
  readButton();
  if (now - windowAt >= WINDOW_MS) endWindow();
  if (now - stepAt >= STEP_MS) {
    stepAt = now;
    if (tracking) {
      stepTracker();
    } else {
      panAngle = PARK_PAN;
      tiltAngle = PARK_TILT;
    }
    pan.write(panAngle);
    tilt.write(tiltAngle);
    readPanel();
    sample();
  }
  if (now - printAt >= 1000) {
    printAt = now;
    report();
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

stepTracker sketch.ino

Reads all four sensors and averages them into left, right, top and bottom. If one side is brighter by more than DEADBAND, which is 20, it moves the pan or tilt angle by one degree toward the bright side. It then limits both angles.

void stepTracker() {
  int tl = analogRead(TL), tr = analogRead(TR), bl = analogRead(BL), br = analogRead(BR);
  int left = (tl + bl) / 2, right = (tr + br) / 2;
  int top = (tl + tr) / 2, bottom = (bl + br) / 2;
  // One degree a step is slow enough to be smooth and quick enough to keep
  // up with the real sun, which moves a quarter of a degree a minute.
  if (left - right > DEADBAND) panAngle--;
  if (right - left > DEADBAND) panAngle++;
  if (top - bottom > DEADBAND) tiltAngle++;
  if (bottom - top > DEADBAND) tiltAngle--;
  panAngle = constrain(panAngle, 0, 180);
  tiltAngle = constrain(tiltAngle, 10, 170);
}

readPanel sketch.ino

Asks the INA219 for the panel's voltage, current and power, and stores them in volts, milliamps and milliwatts for the rest of the sketch to use.

void readPanel() {
  volts = ina.getBusVoltage_V() + ina.getShuntVoltage_mV() / 1000.0;
  milliamps = ina.getCurrent_mA();
  milliwatts = ina.getPower_mW();
}

sample sketch.ino

Adds up measured energy. It takes the power reading and multiplies it by the time since the last sample, then adds that to the total for the current mode, tracked or fixed.

void sample() {
  // Energy is power times time: add up each tenth of a second's worth.
  unsigned long now = millis();
  float hours = (now - sampleAt) / 3600000.0;
  sampleAt = now;
  uint8_t mode = tracking ? 0 : 1;
  energy[mode] += milliwatts * hours;
  seconds[mode] += hours * 3600.0;
  windowEnergy += milliwatts * hours;
}

endWindow sketch.ino

Runs every 20 seconds. It prints the energy from the window that just ended and the running average for each mode. Then it switches between tracking and parked, and swaps the green and yellow LEDs.

void endWindow() {
  Serial.print("== ");
  Serial.print(tracking ? "tracked" : "fixed");
  Serial.print(" window: ");
  Serial.print(windowEnergy, 3);
  Serial.println(" mWh measured");
  Serial.print("== so far: ");
  printAverage(0);
  Serial.print(", ");
  printAverage(1);
  Serial.println();
  windowEnergy = 0;
  tracking = !tracking;
  windowAt = millis();
  digitalWrite(TRACK_LED, tracking ? HIGH : LOW);
  digitalWrite(FIXED_LED, tracking ? LOW : HIGH);
  Serial.println(tracking ? "-- tracking for 20 s" : "-- parked at pan 90, tilt 45 for 20 s");
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The panel turns the wrong way toward the light This project
    Check which sensor is on which pin. Top left is A0, top right is A1, bottom left is A2 and bottom right is A3.
  • The servos keep turning and never settle This project
    The simulated light does not move as the servos turn. Even out the four sensor sliders so the difference falls under DEADBAND.
  • The panel readings are all zero This project
    The panel's plus goes to the INA219 VIN+, and VIN- goes to the 36 ohm load that returns to ground.
  • The reading barely changes, or sits stuck near 0 or the top of the range. Light sensor
    An LDR is a resistor, so use it in a divider with a fixed resistor, about 10k, and read the middle node. Without the divider the analog pin sees nothing useful.
  • Readings go the wrong way: higher in the dark. Light sensor
    Which side the LDR sits on decides the direction. LDR on the supply side gives a higher reading in the light. LDR on the ground side gives a lower reading in the light. Swap them if needed.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The servo jitters, twitches or resets the board when it moves. Hobby servo
    Power it from its own 5 V supply, not the Arduino 5 V pin, and join the grounds. A hobby servo can pull several hundred milliamps. Add a 100 uF to 470 uF capacitor across its supply.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.