this readme are writen using the base code as the main source of writing and explanation, for the detaild explanation for the codes itteration/version u can read the readme on the code folder it self.
- Introduction
- Version
- Setup
- Dependencies
- Configuration
- Operation
- PID Parameters
- Sensor Calibration
- Motor Control
- Serial Output
- Troubleshooting
- Flowchart
- Contributing
- License
This repository contains code for a PID-controlled system that combines an ultrasonic sensor for distance measurement and motor control based on a setpoint. The PID algorithm helps maintain a specified distance from an obstacle by adjusting the motor speed.
this repo contain various version on the code including one with display, auto ajdust pid val using continuous self-tuning and also Ziegler-Nichols method.
vesion | feature | display |
---|---|---|
manual 1 | base version with no special feature | no |
manual 2 | ajdustable pid val using pot | no |
manual 3 | adj version with display and pot | yes |
manual 4 | non adj version with display | yes |
auto 1 | auto tune with continuous tuning mechanism | yes |
auto 2 | auto tune with Ziegler-Nichols method | yes |
Ensure the following connections:
- Trig pin of the ultrasonic sensor connected to pin 3.
- Echo pin of the ultrasonic sensor connected to pin 2.
- Potentiometer connected to analog pin A0.
- Motor control pin connected to pin 9.
This code requires the Arduino IDE and the accompanying Arduino library for ultrasonic sensors. Ensure you have these installed before uploading the code.
Adjust the PID parameters (Kp, Ki, Kd) and setpoint range in the code based on your specific requirements. Also, configure the motor control pin and potentiometer pin according to your hardware setup.
Upon setup, the system uses the ultrasonic sensor to measure distance and adjusts the motor speed to maintain the setpoint distance. The potentiometer allows you to set the desired distance range.
Fine-tune the proportional (Kp), integral (Ki), and derivative (Kd) gains to optimize the system's performance. Experimentation may be required for your specific use case.
Calibrate the setpoint range using the potentiometer. Map the analog readings to the desired distance range for accurate distance control.
The motor control is implemented using PWM. Adjust the PWM range in the code to match the motor specifications and achieve the desired speed control.
The serial output provides real-time information about the setpoint, measured distance, PWM value, and PID output. Use this information for debugging and monitoring the system.
Refer to the troubleshooting section in case of issues. Common problems and solutions are outlined to help you resolve any operational issues.
graph TD
subgraph Initialization
A[Initialize Serial] -->|Baud Rate: 9600| B
B -->|Initialize Pins| C
C -->|Set Motor Pin| D
D -->|Delay 2s| E
end
subgraph Main Loop
E -->|Ultrasonic Sensor| F[Read Distance]
F -->|Map Potentiometer| G[Set Setpoint]
G -->|PID Control| H[Calculate Output]
H -->|Map to PWM| I[Adjust Motor]
I -->|Serial Output| J[Print Data]
J -->|Delay 100ms| E
end
subgraph Ultrasonic Sensor
F -->|Trigger Pulse| K[Send Trigger]
K -->|Echo Duration| L[Receive Echo]
L -->|Calculate Distance| M[Convert to cm]
end
subgraph PID Control
G -->|Calculate Error| N[Error = Setpoint - Distance]
N -->|Update Integral| O[Integral += Error]
N -->|Calculate Derivative| P[Derivative = Error - LastError]
O -->|Calculate Output| Q[Output = Kp * Error + Ki * Integral + Kd * Derivative]
end
subgraph Motor Control
Q -->|Map to PWM| R[Map Output to PWM Range]
R -->|Constrain PWM| S[Constrain PWM Value]
S -->|Set Motor Speed| I
end
subgraph Serial Output
J -->|Print Data| T[Print Setpoint, Distance, PWM, PID Output]
end
Contributions are welcome! Feel free to submit issues, propose enhancements, or fork the repository for your own experiments.
This code is distributed under the MIT License. Feel free to use and modify it for your projects.