Giter Site home page Giter Site logo

lily-open-source / pid-controlled-ultrasonic-distance-measurement Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 39 KB

This repository contains code for a PID-controlled system that combines an ultrasonic sensor for distance measurement and motor control based on a setpoint.

License: MIT License

C++ 100.00%
arduino control-systems pid-control

pid-controlled-ultrasonic-distance-measurement's Introduction

Arduino PID-controlled Brusless motor system with Ultrasonic sensor

this readme are writen using the base code as the main source of writing and explanation.

Table of Contents

  1. Introduction
  2. Setup
  3. Dependencies
  4. Configuration
  5. Operation
  6. PID Parameters
  7. Sensor Calibration
  8. Motor Control
  9. Serial Output
  10. Troubleshooting
  11. Flowchart
  12. Contributing
  13. License

1. Introduction

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.

2. Setup

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.

3. Dependencies

This code requires the Arduino IDE and the accompanying Arduino library for ultrasonic sensors. Ensure you have these installed before uploading the code.

4. Configuration

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.

5. Operation

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.

6. PID Parameters

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.

7. Sensor Calibration

Calibrate the setpoint range using the potentiometer. Map the analog readings to the desired distance range for accurate distance control.

8. Motor 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.

9. Serial Output

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.

10. Troubleshooting

Refer to the troubleshooting section in case of issues. Common problems and solutions are outlined to help you resolve any operational issues.

11. Flowchart

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

12. Contributing

Contributions are welcome! Feel free to submit issues, propose enhancements, or fork the repository for your own experiments.

13. License

This code is distributed under the MIT License. Feel free to use and modify it for your projects.

pid-controlled-ultrasonic-distance-measurement's People

Contributors

1999azzar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.