Adjustable Parameter Bot (Ajusta Bot)
Welcome to the Ajusta Bot Lab
In this lab you will add buttons and a knob to your robot so you can change its settings without rewriting your code. That is a big step toward building a truly interactive robot!
In the previous labs, you changed the robot's speed and turning distance by editing numbers in your code. Every time you changed a value, you had to save the file and restart the robot. This lab fixes that problem.
You will add two buttons and a rotary encoder to the Face Bot. A rotary encoder is a knob you can turn to increase or decrease a value. With these controls, you can adjust settings like forward speed and turning distance while the robot is running — no code changes needed.
What You Need
| Part | Purpose |
|---|---|
| Face Bot from the previous lab | Your base robot with the OLED display |
| 2 push buttons | Select which parameter to adjust |
| Rotary encoder | Turn the knob to increase or decrease the selected value |
| Breadboard and jumper wires | Connect everything together |
What Parameters Can You Adjust?
The robot has several settings (called parameters) that control how it moves. These are the ones you will be able to change with the knob and buttons:
- Forward speed — how fast the motors run when driving forward
- Turn threshold distance — how close an obstacle must be before the robot turns
- Backup time — how long the robot reverses before turning
- Turn time — how long the robot turns before going straight again
Connecting the Buttons
Connect two push buttons to the Pico:
- Connect one leg of Button A to GPIO pin 14.
- Connect the other leg of Button A to GND.
- Connect one leg of Button B to GPIO pin 15.
- Connect the other leg of Button B to GND.
1 2 3 4 | |
What each line does
Pin.IN— sets the pin as an input (it reads a signal)Pin.PULL_UP— holds the pin at HIGH (1) until the button is pressed, then it goes LOW (0)
Key Idea
With PULL_UP, a button reads 1 when not pressed and 0 when pressed. This is opposite to what you might expect, but it is the most reliable way to wire a button without extra resistors.
Connecting the Rotary Encoder
A rotary encoder has three pins for the rotation signal: a common ground (GND), a clock pin (CLK), and a data pin (DT). Connect them as follows:
- Connect the encoder GND pin to the GND rail on the breadboard.
- Connect the encoder CLK pin to GPIO pin 10.
- Connect the encoder DT pin to GPIO pin 11.
1 2 | |
The encoder sends two signals as you turn the knob. By comparing those signals, your code can tell which direction the knob is turning.
Selecting a Parameter with the Buttons
Button A moves to the next parameter. Button B moves to the previous parameter. The OLED display shows which parameter is selected and its current value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
What each line does
param_names— a list of the parameter names you will see on the displayparam_values— a list of the current values for each parameterparam_index— tracks which parameter is currently selected% len(param_names)— wraps the index back to 0 when it reaches the end of the list
Reading the Encoder Direction
This function reads the rotary encoder and returns +1 if you turned the knob clockwise, or -1 if you turned it counter-clockwise:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
What each line does
last_clk— remembers the previous state of the clock pincurrent_clk != last_clk— checks if the clock pin changed (which means the knob moved)encoder_dt.value() != current_clk— compares the data and clock signals to find the direction
Updating the Display
Show the selected parameter and its value on the OLED screen:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
Monty's Tip
Keep the display update fast. If you call oled.show() too often in the main loop, it can slow down the motor control. Call it only when a value changes.
Putting It All Together
Here is the main loop that reads buttons, reads the encoder, updates parameters, and drives the robot:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
What each line does
param_steps— each parameter changes by a different amount per knob clickparam_values[param_index] += direction * step— adds or subtracts one step from the selected value- The last block uses the current parameter values to run the robot
You Can Do This!
Tuning a robot's parameters can take many tries. Turn the knob a little, watch the robot, and adjust again. That is exactly what engineers do when they tune real robots!
You Built a Tunable Robot!
You can now adjust your robot's behavior on the fly with buttons and a knob. You have built one of the most complete robots in this course — well done!