ESP32 Ball and Beam PID Balancer in Proteus Simulation: the ESP32, the ball and beam lab, the knob and the OLED

ESP32 Ball and Beam PID Balancer in Proteus Simulation

40
700
60
25
60
PCBWay

Hello friends, I hope you all are doing great. Today, I am going to share the ESP32 Ball and Beam PID Balancer in Proteus Simulation, the sixty-fifth project of our IoT Projects series. If you have searched for a ball and beam PID project, an ESP32 servo control loop, or a Proteus simulation of a classic control lab, here is a complete one. A table-tennis ball rolls in a 250 mm aluminium channel, an MG996R servo tilts the channel, and a Sharp infrared sensor at one end watches the ball. The ESP32 holds the ball wherever you ask, from a knob or from a dashboard on a laptop or a phone. And before it balances anything, it does what a control engineer would do: it measures the lab, works out the model from the measurement, and designs its own gains.

The ball and beam is the most famous balancing experiment of control courses, because the ball never settles by itself: tilt the beam a little and the ball keeps speeding up until it hits the end. In this tutorial, we will go through the circuit, see why a rolling ball accelerates less than a sliding one, read the ball's position through the infrared sensor's curved response, run a tilt test and fit the ball's path to find the plant's gain, and turn that number into PD gains for a chosen overshoot and settling time. We will also meet two traps of real measurements on the way. Then we run the simulation: the self-test, steps to new positions, a faster design, a sine to follow, and the knob. At the end, you can download the project with the sketch and every library file.

NOTICE: Our team has put a lot of effort into this project: the circuit, the code and every part in the simulation are our own work, and you get them all free. Your feedback is the fuel that keeps us going, so please tell us what you think in the comments below. And if our free projects help you, you can buy us a coffee. So, let's get started with the ESP32 ball and beam PID balancer in Proteus simulation:

ESP32 Ball and Beam PID Balancer in Proteus Simulation: the ESP32, the ball and beam lab, the knob and the OLED
Figure: The ball and beam in a Proteus simulation: the ESP32 holding the ball at 16 cm.
ESP32 Ball and Beam PID Balancer: the working Proteus simulation, an animated view of the project's main steps
Figure: The project working in the Proteus simulation (an animation of the main steps).

What Does This Ball and Beam Balancer Do?

  • Reads the ball: the Sharp GP2Y0A21 infrared sensor gives a voltage that falls as the ball moves away; the ESP32 turns it back into centimetres with the sensor's own curve.
  • Tilts the beam: an MG996R servo lifts the beam's far end through a short horn and a link; the ESP32 sets its angle with a 50 Hz pulse.
  • Measures the lab first: with the ball still, it tilts the beam 5 degrees one way and then the other, records the ball's path, and fits it to find how fast the ball accelerates for each degree of the servo.
  • Designs its own gains: from that one number, a PD controller for 5 % overshoot and the settling time you choose: 2, 3 or 5 seconds.
  • Measures every move: the rise time, the overshoot, the peak time and the settling time of each new position, like a lab report.
  • Takes its setpoint from you: a knob for 10 to 22 cm, the dashboard's 12, 16 and 20 cm, or a sine that keeps the ball moving.
  • Shows it all: a 0.91" OLED with the mode, the setpoint, the ball, the servo and the last move, and a dashboard with the model and the gains.

It is a great project for learning feedback control the practical way: a model from physics, a measurement to check it, a controller designed from the model, and every result measured. In the simulation, the lab's pop-up shows the ball, the beam and the servo, its own model of the plant and the gains it suggests, and its own measurement of every move, so you can check each of the ESP32's numbers against an independent judge.

Components Used in the Proteus Simulation

All the parts come from our Proteus libraries, each with its own tutorial. The project zip at the end includes every LIB and DLL file.

The parts of the ball and beam balancer and their library posts
PartProteus deviceLibrary postConnection
ESP32 DevKit V1ESP32 DEVKIT V1ESP32 DevKit V1 Library for ProteusU1, ESP32_Ball_Beam.hex, TEP-WiFi 192.168.1.101
Ball and beam lab (MG996R + GP2Y0A21)BALLBEAMTEPBall and Beam PID Control Lab Library for ProteusU2: SIG from D13, VOUT to D34, 5V, GND
10k potentiometer (the knob)POTROTARY16TEPPotentiometer Library for ProteusRV1, across 3.3 V, the wiper to D35
SSD1306 OLED 0.91" 128x32OLED12832WHITEADVTEPOLED 128x32 Library for ProteusOLED1, I2C at 0x3C
Laptop WiFi MonitorLAPTOP WIFI MONITORLaptop WiFi MonitorPC1
Mobile WiFi MonitorMOBILE WIFI MONITORMobile WiFi MonitorMOB1

Proteus Simulation of the Ball and Beam: the Circuit

Ball and beam Proteus simulation circuit: ESP32 DevKit V1, the ball and beam lab, a potentiometer and a 128x32 OLED
Figure: The complete circuit of the balancer on an A3 Proteus sheet.
The connections of the ball and beam balancer
PartPinsESP32 / supply
U2 the labSIG / VOUTD13 / D34
U2 the lab5V / GND+5V / GND
RV1 the knob1 / W / 3GND / D35 / +3.3V
OLED1SDA / SCL / VCCD21 / D22 / +3.3V

The lab is drawn at true size, 320 mm long, so the project needs an A3 sheet. Its four pins sit on the bottom edge of the base. The sensor's output goes to D34, one of the ESP32's ADC1 pins: on a real ESP32 the ADC2 pins cannot be read while Wi-Fi is on. The GP2Y0A21 gives at most about 3.2 V, which is inside the ESP32's 3.3 V input range, so no divider is needed. The lab's 5V pin feeds both the sensor and the servo, whose current grows with its speed, so it gets a real 5 V supply rather than a net label. The knob is a plain voltage divider across 3.3 V, and the OLED shares the I2C bus on D21 and D22.

How the Ball and Beam Balancer Works

The Physics: a Ball Rolling on a Tilted Beam

A block sliding without friction down a slope tilted by an angle alpha accelerates at g sin(alpha). A rolling ball accelerates less, because part of the push from gravity goes into spinning it. For a thin shell like a table-tennis ball, the moment of inertia is 2/3 m r squared, and rolling on the channel's two rims, 10 mm either side of the middle, it touches the rims at 17.32 mm from its centre instead of its full 20 mm radius. The result is a = g sin(alpha) / (1 + 2/3 x (20 / 17.32) squared) = 981 / 1.889 = 519.4 cm/s2 times sin(alpha), only 0.53 g. The beam's angle follows the servo through the linkage: a 20 mm horn lifts a link 110 mm from the pivot, so alpha = asin(20 sin(theta) / 110), about 0.18 of the servo's angle theta. Worked example for 5 degrees of servo: alpha = asin(20 x 0.0872 / 110) = 0.908 degrees, and a = 519.4 x sin(0.908 deg) = 8.23 cm/s2. Divided by 5, that is 1.65 cm/s2 for each degree of the servo: the plant's gain K.

Reading the Ball: the GP2Y0A21's Curve

The Sharp sensor measures distance by triangulation, and its output voltage falls with the distance along a curve, not a straight line: about 2.30 V at 10 cm, 1.98 V at 12 cm, 1.65 V at 15 cm and 1.30 V at 20 cm. Between the datasheet's points the curve is close to a straight line on a log-log scale, so the sketch interpolates there. Worked example: 1.81 V lies between 12 cm (1.98 V) and 15 cm (1.65 V); the fraction is ln(1.81 / 1.98) / ln(1.65 / 1.98) = 0.49, and the distance 12 x (15 / 12) to the power 0.49 = 13.4 cm. The sensor sees the ball's near surface, so the ball's centre is 2 cm further: 15.4 cm. Closer than about 6 cm the curve folds back and two distances give the same voltage, so the setpoints stay between 10 and 22 cm.

Measuring the Lab: the Tilt Test

The ESP32 does not take K on trust. It waits until the ball is still, tilts the beam 5 degrees for 0.7 s, then 5 degrees the other way for 0.7 s so that the ball stops again, and records the ball's path. A ball starting from rest under a constant acceleration follows a parabola, d = a t squared / 2, so the path gives a. Two lessons came out of building this. First, the sensor gives a new reading every 38.3 ms; read on a 40 ms loop, each reading arrives a different time after it was taken, and in an offline model of the lab that jitter alone moved a fitted parabola anywhere from 7.0 to 10.5 cm/s2 for the true 8.2. So during the test the ESP32 samples every 5 ms and keeps each new reading with its own time. Second, the servo and the sensor add a short delay before the ball seems to move, and a free three-term parabola fitted to noisy readings over 0.6 s still wandered from 5.5 to 8.7 cm/s2. Since the ball starts still, the sketch fits d = a (t - tau) squared / 2 instead: for every delay tau from 0 to 150 ms the best a has a simple formula, and the tau with the smallest error wins. Twenty simulated tests gave 8.04 cm/s2 on average for the true 8.23.

Designing the PD from K

Near the level, the ball obeys x'' = -K theta: the servo's angle sets the ball's acceleration, and nothing pulls the ball back by itself. A PD controller sets theta = Kp (x - setpoint) + Kd x speed. Put together, x'' + K Kd x' + K Kp x = K Kp x setpoint, which is the standard second-order system with wn squared = K Kp and 2 zeta wn = K Kd. A 5 % overshoot needs a damping zeta of 0.69, and the settling time to within 2 % is about 4 / (zeta wn). Worked example for 3 s with K = 1.644: wn = 4 / (0.69 x 3) = 1.93 rad/s, Kp = 1.93 squared / 1.644 = 2.27 degrees per cm, Kd = 2 x 0.69 x 1.93 / 1.644 = 1.62 degrees per cm/s. For 2 s, wn = 2.90 rad/s, Kp = 5.11 and Kd = 2.43: twice the stiffness for one third less time.

The Derivative, the Speed and the Servo's Limit

The D term uses the ball's speed, not the change of the error, so a new setpoint does not kick the servo: only the P term jumps. The speed comes from two readings and the time between them, smoothed a little (0.6 of the old value plus 0.4 of the new) because the sensor has a few millivolts of noise. The servo is limited to plus or minus 30 degrees, a beam angle of about 5.2 degrees; a big step with the 2 s gains asks for more than that at the start, and the limit simply holds until the ball is under way.

Measuring Each Move

Every new setpoint starts a measurement: the time from 10 % to 90 % of the move (the rise time), how far the ball goes past the target (the overshoot, as a percentage of the move), when that peak comes, and when the ball enters a band around the target for good (the settling time). The band is 2 % of the move, but never less than 0.15 cm: the sensor's noise is a few hundredths of a centimetre, and the first test run with a pure 2 % band, 0.08 cm for a 4 cm move, said 7.2 s because the noise kept stepping out of it. The ball counts as settled when it has stayed in the band for 1.5 s.

Following a Sine

In SINE mode the setpoint moves smoothly, 16 cm plus or minus 4 cm with a period of 10 s. A PD loop follows a moving target with a small lag, because it only acts on an error once there is one; the ESP32 adds up the squared error after the first period and shows the root-mean-square on the dashboard.

The Arduino Code Explained

The sketch is ESP32_Ball_Beam.ino, written in the Arduino IDE for the ESP32 DevKit V1. It uses WiFi and WebServer for the dashboard, ESP32Servo for the MG996R, and Adafruit SSD1306 for the OLED. The sensor's curve, the ball's near surface in centimetres and the voltage at each:

const float CM[] = {6, 7, 8, 9, 10, 12, 15, 20, 25, 30};            // the GP2Y0A21's typical curve: the ball's near surface, cm ...
const float VV[] = {3.15, 3.05, 2.80, 2.55, 2.30, 1.98, 1.65, 1.30, 1.08, 0.92};   // ... and its output, V

A reading becomes the ball's centre, interpolated on the log-log scale:

  float s = log(v / VV[k]) / log(VV[k + 1] / VV[k]);                // (between two points: straight on a log-log scale, as the datasheet)
  return CM[k] * pow(CM[k + 1] / CM[k], s) + 2;

The servo: 1500 us is the beam level, 11.1 us a degree, limited to plus or minus 30 degrees:

void setServo(float deg) { u = constrain(deg, -LIMIT, LIMIT); servo.writeMicroseconds(1500 + u * US_PER_DEG); }

During the tilt test, each new sensor reading is stored with its own time:

    if (r != last && n < MAXP) { last = r; pt[n] = t; pd[n] = r - x0; n++; }   //  cm/s2 for -8.2)

The fit: for each delay, the best acceleration for d = a (t - tau) squared / 2:

    for (int k = 0; k < n; k++) { float q = pt[k] > tt ? 0.5 * (pt[k] - tt) * (pt[k] - tt) : 0; sq += q * q; sd += q * pd[k]; }

The plant's gain from the acceleration (a positive angle rolls the ball towards the sensor, so a is negative):

  if (n >= 8 && a < -1) K = -a / TUNE_DEG;                          // (+ deg lifts the far end: the ball rolls TOWARDS the sensor, a < 0)

The PD design for 5 % overshoot and the settling time asked:

  float wn = 4 / (ZETA * ts);
  kp = wn * wn / K; kd = 2 * ZETA * wn / K;

The speed from two readings and the real time between them, smoothed:

  if (dt > 0.005) speed = 0.6 * speed + 0.4 * (x - xPrev) / dt;     // (a little filtering: the sensor's noise)

The control law itself, one line every 40 ms:

  setServo(kp * (x - ref) + kd * speed);                            // the ball too far (x > ref) lifts the far end: it rolls back

The settling band of a move, never under the sensor's noise:

  if (fabs(x - m1) > max(0.02 * fabs(span), 0.15)) tOut = now;     // (2 % of a 4 cm move is 0.08 cm, under the sensor's noise: probe 9

The sine setpoint:

    ref = 16 + 4 * sin(2 * PI * (now - sineT0) / 10000.0);

One web handler serves every page and reads the address itself, which keeps the sketch light:

  else if (p == F("/s12") || p == F("/s16") || p == F("/s20")) { mode = WEB_M; startMove(p.substring(2).toInt()); }

The loop runs every 40 ms, just slower than the sensor's 38.3 ms cycle. The OLED gets a new frame once a second: a frame is 512 bytes over I2C at the standard 100 kHz, and an early test with the bus at 400 kHz made the simulation crawl. The serial monitor gets the ball, the setpoint and the servo every 5 s, and every event. The pages are / (the dashboard), /knob and /sine (the modes), /s12, /s16 and /s20 (the setpoints), /ts2, /ts3 and /ts5 (the settling time), /tune (the tilt test again) and /data (JSON).

How to Run the Proteus Simulation

  1. Download the project zip below and install the library files (the steps are under the download button).
  2. Open ESP32-Ball-Beam.pdsprj from the Proteus Simulation folder. Keep the HEX and DLL files next to it.
  3. Press Play. The ESP32 waits for the ball to be still, runs the tilt test, designs the PD and moves the ball to the knob's 16 cm.
  4. Click the lab (U2) for its pop-up: compare its model, its gains and its measurement of each move with the ESP32's.
  5. Open the Laptop or the Mobile WiFi Monitor and press Go: try 20 cm, then 2 s and 12 cm, then SINE.
  6. Press KNOB, and on the knob's pop-up (RV1) press 100 %: the ball goes to 22 cm.

Simulation Results: the Ball and Beam in Proteus

Here is the run that made the screenshots of this tutorial, 63 seconds of simulation: the tilt test, four moves, a faster design, a sine and the knob. The times are the sketch's own clock, as it printed them on the serial monitor; the lab's numbers come from its pop-up, which watches the real ball in the model, not the sensor.

The Tilt Test and the Gains

At 3.3 s the ball has been still at 15.5 cm for a second, and the beam tilts. 0.7 s later it tilts back, and at 4.9 s the result: "TUNE: a -8.21 cm/s2 after a delay of 74 ms, from 19 readings - K 1.643 cm/s2 per servo degree". The physics above gave 8.23 cm/s2 and 1.65; the lab's own pop-up says 1.648. From K, the PD for 5 % overshoot and a 3 s settling time: "wn 1.93 rad/s, Kp 2.27 deg/cm, Kd 1.62 deg per cm/s", the very gains the pop-up recommends. The test leaves the ball nearer the sensor, at 11.8 cm, and the knob at 50 % asks for 16 cm: at 7.7 s, "move 11.8 to 16.0 cm: rise 0.88 s, overshoot 2.3 %, peak at 1.61 s, settled in 1.25 s".

The ESP32 ball and beam Proteus simulation during the tilt test: the beam tilted, the ball rolling
Figure: The tilt test.
The ESP32 ball and beam Proteus simulation: the ball held at the knob's 16 cm
Figure: The ball at 16 cm.
Ball and Beam pop-up in the Proteus simulation: HOLDING at 16.0 cm, the model 1.648 and the gains 2.27 and 1.62
Figure: The lab: holding 16 cm.
OLED pop-up in the Proteus simulation: KNOB 3S, SET 16.0, BALL 16.0 CM, OS 2.3 % TS 1.2 S
Figure: The OLED: KNOB.
Laptop WiFi Monitor in the Proteus simulation: the ball and beam dashboard, K 1.643 measured
Figure: The dashboard after the test.

A Step to 20 cm

20 cm on the laptop at 11.0 s, and at 13.7 s: "move 16.0 to 20.0 cm: rise 0.84 s, overshoot 3.0 %, peak at 1.59 s, settled in 1.27 s". The lab's pop-up measured the same move at 0.4 % overshoot and 0.91 s. The difference is the sensor: the pop-up sees the ball itself, while the ESP32 sees it through a few millivolts of noise, and the highest reading of a noisy signal always looks a little higher than the truth. A 0.1 cm hump is 2.5 % of a 4 cm move. The settling time is shorter than the 3 s design because the band of 0.15 cm is wider than 2 % of this move.

The ESP32 ball and beam Proteus simulation: the ball moved to 20 cm
Figure: The ball at 20 cm.
Ball and Beam pop-up in the Proteus simulation: HOLDING at 20.0 cm, its own measurement 0.4 % and 0.91 s
Figure: The lab measures the move itself.
Laptop dashboard in the Proteus simulation: 16.0 to 20.0 cm, rise 0.84 s, overshoot 3.0 %, settled in 1.27 s
Figure: The dashboard: the move measured.

A Faster Design: Settling in 2 s

2 s on the laptop at 16.9 s, and the ESP32 redesigns at once: "wn 2.90 rad/s, Kp 5.11 deg/cm, Kd 2.43 deg per cm/s". Then 12 cm at 18.1 s, an 8 cm move that asks the servo for 41 degrees at first, more than its 30 degree limit. At 21.5 s: "move 20.0 to 12.0 cm: rise 0.52 s, overshoot 5.4 %, peak at 1.04 s, settled in 1.88 s". The pop-up measured 5.9 % and 1.60 s. The rise time has dropped from 0.84 s to 0.52 s and the settling time is under the 2 s asked; the overshoot is a little over the 5 % design, partly from the servo's limit at the start of the move.

Laptop dashboard in the Proteus simulation: settling 2 s, the new gains Kp 5.11 and Kd 2.43
Figure: The dashboard: the 2 s design.
The ESP32 ball and beam Proteus simulation: the faster move to 12 cm with the 2 s gains
Figure: The ball at 12 cm.
Ball and Beam pop-up in the Proteus simulation: HOLDING at 12.0 cm, the last move 5.9 % and 1.60 s
Figure: The lab: the 12 cm move.
OLED pop-up in the Proteus simulation: WEB 2S, SET 12.0, BALL 12.0 CM, OS 5.4 % TS 1.9 S
Figure: The OLED: the faster move.

Following a Sine

SINE at 24.2 s sets the target swinging between 12 and 20 cm every 10 s. The ball follows it with a small lag, and the pop-up's graph draws the ball's path as a clean wave just behind the setpoint. After the first period the ESP32 starts adding up the error, and the dashboard shows an RMS error of 0.84 cm: the PD only reacts once an error exists, so a moving target always stays a little ahead.

The ESP32 ball and beam Proteus simulation in SINE mode: the ball following 16 cm plus or minus 4 cm
Figure: The ball following a sine.
Ball and Beam pop-up in the Proteus simulation: the ball tracing a sine on the graph since the start
Figure: The lab: the sine on the graph.
Laptop dashboard in the Proteus simulation: SINE, settling 2 s, the RMS error 0.84 cm
Figure: The dashboard: the tracking error.
Mobile WiFi Monitor in the Proteus simulation: the ball and beam dashboard in SINE mode
Figure: The phone: SINE.

Back to the Knob

KNOB at 50.3 s brings the target back to the knob's 16 cm while the ball is still moving from the sine: "move 14.5 to 16.0 cm: rise 0.32 s, overshoot 10.7 %". The overshoot is large because the move started with the ball already rolling towards the target, not from rest. Then the knob goes to 100 % on its pop-up, 3.29 V on the wiper, which is 22 cm: at 59.2 s, "move 16.0 to 22.0 cm: rise 0.44 s, overshoot 6.6 %, peak at 0.96 s, settled in 2.40 s". The pop-up measured 5.1 % and 1.48 s. The serial monitor holds the whole story, line by line.

Laptop dashboard in the Proteus simulation: KNOB, the setpoint back to 16.0 cm
Figure: The dashboard: KNOB.
The ESP32 ball and beam Proteus simulation: the knob at 100 %, the ball held at 22 cm
Figure: The ball at 22 cm.
Potentiometer pop-up in the Proteus simulation: the knob at 100 %, 3.29 V on the wiper
Figure: The knob at 100 %.
Ball and Beam pop-up in the Proteus simulation: HOLDING at 22.0 cm, the last move 5.1 % and 1.48 s
Figure: The lab: holding 22 cm.
OLED pop-up in the Proteus simulation: KNOB 2S, SET 22.0, BALL 22.0 CM, OS 6.6 % TS 2.4 S
Figure: The OLED at 22 cm.
Laptop dashboard in the Proteus simulation: the last move, 16.0 to 22.0 cm measured
Figure: The dashboard: the last move.
ESP32 serial monitor in the Proteus simulation: the tilt test, the gains and every move
Figure: The serial monitor: the whole run.
The results of the ball and beam simulation
WhatThe ESP32The lab's pop-up
The plant's gain K1.643 cm/s2 per degree (a = -8.21 cm/s2, delay 74 ms)1.648
PD for 3 sKp 2.27, Kd 1.62Kp 2.27, Kd 1.62
16 to 20 cm (3 s)rise 0.84 s, overshoot 3.0 %, settled in 1.27 s0.4 %, 0.91 s
20 to 12 cm (2 s)rise 0.52 s, overshoot 5.4 %, settled in 1.88 s5.9 %, 1.60 s
16 to 22 cm (2 s)rise 0.44 s, overshoot 6.6 %, settled in 2.40 s5.1 %, 1.48 s
SINE 16 +- 4 cm, 10 sRMS error 0.84 cm-
Simulation63 s of simulation-

Download the Project Files

Click the button below to download ESP32-Ball-and-Beam-PID-Balancer-Proteus-Simulation-v1.0.zip (about 3.8 MB, without the source code of the models):

ESP32 Ball and Beam PID Balancer in Proteus Simulation V1.0
  • Proteus Simulation: ESP32-Ball-Beam.pdsprj, ESP32_Ball_Beam.hex and copies of the models.
  • Arduino Code: ESP32_Ball_Beam.ino, the sketch.
  • Proteus Library Files: the LIB files of the ESP32 DevKit V1, the ball and beam lab, the potentiometer, the OLED and the two WiFi Monitors.
  • Proteus Model Files: the DLL models.
  • README.txt: the installation steps, the properties, the test results and the links to every library post.

No Proteus yet? Read How to Download and Install Proteus 7 and 8. New to libraries? Follow How to Install a New Library in Proteus.

How to Install the Project Files

  1. Close Proteus and extract the whole zip file, for example to your Desktop.
  2. Copy the files from Proteus Library Files into the LIBRARY folder of Proteus.
  3. Copy the files from Proteus Model Files into the MODELS folder.
  4. Start Proteus and open the project from the Proteus Simulation folder.

Changing the Code

Open ESP32_Ball_Beam.ino in the Arduino IDE with the TEP boards for Proteus installed, compile it, export the compiled binary and set U1's PROGRAM property to the new HEX file. The test tilt and its time, the damping, the servo's limit and the loop period are at the top. In U2's properties, BALLX sets where the ball starts, NOISE the sensor's noise in millivolts, and TSET the settling time the pop-up designs for.

Ideas to Extend the Project

  • Add an integral term with anti-windup and see whether it helps with a beam that is not perfectly level.
  • Replace the PD with a state-feedback controller and an observer for the ball's speed.
  • Run the tilt test at several positions along the beam and check that K does not change.
  • Add a step-response chart to the dashboard, drawn from the logged positions.
  • Disturb the ball with a second servo or a fan and compare the PD with a PID.
  • Tune by a relay test instead of the model, and compare the two sets of gains.

From Simulation to Real Hardware

  • Calibrate the sensor: the GP2Y0A21's curve changes from part to part and with the ball's colour; record the voltage at a few known distances and use your own table.
  • Filter more: a real sensor is noisier than the model; average a few readings or add a small capacitor on VOUT.
  • Mind the servo: an MG996R draws over 1 A when it starts; give it its own 5 V supply and join the grounds.
  • Level the beam: a beam that is not level at 1500 us leaves a steady offset with a PD; adjust the horn, or add the integral term.
  • Keep the test small: a 5 degree tilt for 0.7 s moves the ball about 2 cm; on a shorter beam, use less.

Troubleshooting

  • The ball reads wrong: VOUT must go to an ADC1 pin (D34); closer than 6 cm the sensor's curve folds back.
  • The beam does not move: SIG on D13, and a real 5 V supply on the lab's 5V pin.
  • The ball oscillates: run the tilt test again with the ball still (TUNE), or choose the slower 5 s.
  • The OLED stays blank: SDA / SCL on D21 / D22, address 0x3C, VCC on 3.3 V.
  • The simulation runs slowly: keep the I2C bus at its standard 100 kHz.

Limitations of the Simulation

  • The lab is the library's model: a thin shell rolling without slipping, small rolling losses, a servo with a speed limit, and the sensor's typical curve with a little noise.
  • The ball never leaves the channel and bounces gently off the end stops.
  • Real servos have backlash and a dead band that the model leaves out.
  • The network to the laptop and the phone is simulated inside Proteus.
  • ESP32 is a product of Espressif Systems; this project is not made or endorsed by them or by Labcenter.

So, that was all about the ESP32 Ball and Beam PID Balancer in Proteus Simulation. I hope it helps you understand how a rolling ball turns into a simple model, how a short tilt test measures that model, how PD gains follow from it for the overshoot and the settling time you want, and why careful timing matters as much as the maths, all tested in a simulation before you build a real lab. More IoT projects with the ESP32 and the Raspberry Pi 5 are coming in this series. If you have any questions, ask in the comments and I will help you out. Till the next tutorial, take care and have fun!


Comments

0

Join the conversation