Skip to main content
Time: 11:10 AM – 11:35 AM

Understanding servos and calibration

What is a servo?

A servo is a motor that can rotate to a specific angle on command. Unlike a regular motor that just spins continuously, a servo holds a precise position — for example, “turn to exactly 15 degrees left.” Your PiCar-X has three servos: Each servo has a zero position — the angle it considers “center.” Calibration defines where that center actually is for your specific robot.

Why calibrate?

Every robot is slightly different due to manufacturing tolerances. Without calibration:
  • A “go straight” command might actually steer 5° to the left
  • The camera might be tilted slightly down when it should be level
  • Line-following code won’t detect tape correctly
Calibration measures these offsets and saves correction values. Once saved, every Python script automatically applies your calibration — you never have to think about it again.
Calibration values are saved to /opt/picar-x/picar-x.conf. This file persists across reboots and is loaded automatically whenever any script creates a Picarx() object.

The calibration script

Run the calibration script with:
When it starts, you see the current saved calibration values and a menu:
If your values already show non-zero numbers (like Steering : -3.0°), that means someone previously calibrated this robot. You can still re-calibrate — the new values overwrite the old ones.
Mode 1 sets all three servos to their mechanical 0° position. This is used during robot assembly so you can attach the servo horns (the plastic arms) at the correct angle.You probably don’t need this — your robots are pre-assembled. If a mentor asks you to run it, press 1, then press Enter when prompted. The servos snap to 0° and hold there until you press Ctrl+C.
Never rotate a servo by hand while it is powered on. This can damage the gears inside the servo. Always use the script to move servos.

Servo calibration (mode 2)

Press 2 at the main menu, then press Enter to begin. The script walks you through four steps in order: steering, camera pan, camera tilt, and motor direction.

Step 1: Steering servo

The steering servo controls which direction the front wheels point. Your goal is to make the car drive in a perfectly straight line. Controls: The gauge display: As you adjust, you see a live gauge:
  • The | in the center represents 0° (no offset)
  • The # shows your current offset position
  • The number (-8.0) is your exact offset in degrees
  • (1deg) shows your step size — press f to switch to (FINE) for 0.5° steps
How to calibrate steering:
1

Start the car driving

Press t to toggle continuous driving. The car starts moving forward at low speed. It keeps driving until you press t again.
2

Watch the car's path

Look at the car from behind. Does it veer left or right?
3

Nudge the steering

While the car is still driving, press a or d to adjust:
  • Car veers right → press a to nudge left
  • Car veers left → press d to nudge right
The car’s steering updates in real time as you press the keys.
4

Fine-tune

Once it’s close, press f to switch to fine mode (0.5° steps). Make small adjustments until the car tracks straight.
5

Verify

Press t to stop driving, then press Space for a clean 3-second test drive. Watch the car — it should drive forward in a straight line for the full 3 seconds without curving.If it still curves, keep adjusting and test again.
6

Accept

When you’re satisfied, press Enter to lock in the value and move to the next servo.
The key technique is to adjust while the car is driving. Press t to start driving, then tap a/d and watch the car correct in real time. This is much faster than stop-adjust-test-repeat.
How to know it’s correct: The car drives forward in a straight line for at least 3 seconds without veering left or right. A very slight drift is okay — perfection isn’t possible with these servos.

Step 2: Camera pan servo

The camera pan servo rotates the camera left and right. Your goal is to make the camera face directly forward when at its zero position. Controls: How to calibrate camera pan:
1

Look at the camera from above

Stand over the robot and look down. The camera should point straight ahead, centered between the two side plates of the chassis.
2

Nudge until centered

Press a or d to rotate the camera until it faces directly forward.
3

Wiggle to verify

Press w to wiggle. The camera sweeps 15° left, then 15° right, then returns to center. Watch whether it sweeps equally in both directions. If the sweep looks lopsided (more to one side), nudge and wiggle again.
4

Accept

Press Enter when the camera is centered.
How to know it’s correct: When you press w, the camera sweeps equally far to the left and to the right from its center position. The camera lens faces the same direction as the front of the car.

Step 3: Camera tilt servo

The camera tilt servo angles the camera up and down. Your goal is to make the camera level — parallel to the ground, looking at the horizon. Controls: Same as camera pan (a/d, f, r, w, Enter). How to calibrate camera tilt:
1

Look at the camera from the side

Crouch down to eye level with the robot and look at the camera from the side. The camera lens should point straight ahead, not angled up toward the ceiling or down toward the floor.
2

Nudge until level

Press a or d to tilt the camera until the lens is roughly parallel to the ground.
3

Wiggle to verify

Press w to wiggle. The camera should tilt equally up and down from its center position.
4

Accept

Press Enter when the camera is level.
How to know it’s correct: The camera lens is roughly parallel to the ground. When you wiggle, it tilts equally up and down. If you later run a vision script, the camera sees straight ahead — not the ceiling or the floor.

Step 4: Motor direction

The script now tests whether the motors spin in the correct direction. “Forward” should mean the car moves away from you (in the direction the camera faces).
1

Test the motors

Press t. The car drives for 3 seconds, then stops.
2

Check the direction

Did the car move forward (away from you, in the direction the camera points)?
  • Yes, it went forward → press y to confirm.
  • No, it went backward → press n to swap the motor direction.
3

Re-test if you swapped

If you pressed n, press t again to verify the car now drives forward correctly, then press y.
Place the car on the floor with some open space before pressing t. The car will physically drive for 3 seconds.

Save your calibration

After all four steps, the script shows a summary:
Type y and press Enter to save. You see:
If you type n, your calibration is discarded and you have to start over. Always type y unless you made a mistake.

Grayscale sensor calibration (mode 3)

Run the calibration script again and press 3 at the main menu:

What is the grayscale sensor?

On the bottom of your PiCar-X, there are 3 infrared sensors arranged in a row — left, center, and right. Each sensor shines an infrared light downward and measures how much light bounces back. These sensors are used for two things:
  1. Line following — detecting a strip of dark tape on a light floor so the car can follow it
  2. Cliff detection — detecting when there’s nothing below (like a table edge) so the car can stop before falling

Step 1: Line tracking calibration

The car needs to learn what “dark tape” and “light floor” look like to its sensors. It does this by physically driving over the tape and recording the sensor readings. What you need: A strip of dark tape (black electrical tape works well) on a light-colored floor, at least 15 cm (6 inches) long.
1

Position the car over the tape

Place the car so the center sensor is directly over the dark tape and the left and right sensors are on the light floor.
2

Press Enter to start

The script says “Press ENTER to start auto-calibration…” — press Enter. The car starts moving on its own.
3

Watch the car sweep

The car automatically steers left and drives forward, then backward, then steers right and repeats. This wiggling motion sweeps each sensor across the tape so every sensor sees both “dark tape” and “light floor.”Keep the area around the tape clear so the car doesn’t bump into anything.
4

Review the results

After sweeping, the car stops and prints sensor ranges:
The min is what the sensor read over the dark tape. The max is what it read over the light floor. The line reference is the midpoint — any reading below this threshold means “on the line.”
You don’t need to touch any controls during the sweep — the car handles everything automatically. Just make sure it doesn’t drive off the tape area entirely.

Step 2: Cliff detection calibration

Now the car needs to learn what “nothing below” looks like. When the sensors see open air (no surface reflecting light back), the readings drop very low.
1

Pick up the car

Lift the car off the table so nothing is below the sensors — hold it in the air with the bottom facing down.
2

Press Enter

The script says “Press ENTER when suspended…” — press Enter while holding the car in the air.
3

Hold steady for 2 seconds

The script takes 10 readings over 2 seconds and averages them. Keep the car steady with nothing below it.
Values around 5 mean the sensors see almost nothing — exactly what “cliff” looks like.
4

Review the cliff reference

The script calculates a cliff threshold between the “in air” readings and the darkest surface readings from step 1:
Any reading below this threshold triggers cliff detection.

Testing the calibration

Before saving, you can test whether the calibration works correctly. The script shows:
Press t to start live sensor readings. You see a table that updates every half-second:
1

Test on the light floor

Place the car on the light floor, away from the tape.Expected: Line = [0, 0, 0] (no line detected), Cliff = no
2

Test on dark tape

Place the car so the center sensor is over the dark tape.Expected: Line = [0, 1, 0] (center sensor detects line), Cliff = no
3

Test in the air

Pick the car up so nothing is below the sensors.Expected: Cliff = YES
Press Ctrl+C to stop live readings when you’re done testing.
How to know it’s correct:
  • Light floor → Line [0, 0, 0], Cliff no
  • Center on tape → Line [0, 1, 0], Cliff no
  • In the air → Cliff YES
If all three tests pass, your grayscale calibration is working.

Save grayscale calibration

Type y and press Enter to save:
If you type n, the grayscale calibration is discarded and you have to redo both line tracking and cliff detection.

Verify calibration persisted

After saving, confirm your values actually stuck by restarting the script:
Check the startup output. It should show your values, not the defaults:
If you see your calibration values in the startup output, calibration is saved and will automatically apply to every script that uses Picarx(). You can press q to quit.
If the values show all zeros, the save didn’t work. Re-run the calibration and make sure you type y when prompted to save. Also verify you ran the script with sudo — without it, the script may not have permission to write the config file.