0% found this document useful (0 votes)
5 views

Arduino PID Implementation · GitHub

Uploaded by

Alabi Olihide
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Arduino PID Implementation · GitHub

Uploaded by

Alabi Olihide
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Instantly share code, notes, and snippets.

careyi3 / README.md
Last active last week

Code Revisions 5 Stars 23 Forks 5

Arduino PID Implementation

README.md

Arduino PID
This is a basic implementation of a PID controller on an Arduino.

To replicate this, wire up the system as shown below:

https://gist.github.com/careyi3/02a57dfd3a62a96d46171489c83488bd 23/05/2024 09 41
Page 1 sur 5
:
For more info, check out the YouTube video here.

arduino_pid_system.png

https://gist.github.com/careyi3/02a57dfd3a62a96d46171489c83488bd 23/05/2024 09 41
Page 2 sur 5
:
circuit.png

ArduinoNano
||||

D9
D81
D7
w
Ð6
D5

⽩︖
D4 -OUTPUT
D3
D2

C1
22u

https://gist.github.com/careyi3/02a57dfd3a62a96d46171489c83488bd 23/05/2024 09 41
Page 3 sur 5
:
pid.ino

1 const int INPUT_PIN = A0;


2 const int OUTPUT_PIN = DD3;
3
4 double dt, last_time;
5 double integral, previous, output = 0;
6 double kp, ki, kd;
7 double setpoint = 75.00;
8
9 void setup()
10 {
11 kp = 0.8;
12 ki = 0.20;
13 kd = 0.001;
14 last_time = 0;
15 Serial.begin(9600);
16 analogWrite(OUTPUT_PIN, 0);
17 for(int i = 0; i < 50; i++)
18 {
19 Serial.print(setpoint);
20 Serial.print(",");
21 Serial.println(0);
22 delay(100);
23 }
24 delay(100);
25 }
26
27 void loop()
28 {
29 double now = millis();
30 dt = (now - last_time)/1000.00;
31 last_time = now;
32
33 double actual = map(analogRead(INPUT_PIN), 0, 1024, 0, 255);
34 double error = setpoint - actual;
35 output = pid(error);
36
37 analogWrite(OUTPUT_PIN, output);
38
39 // Setpoint VS Actual
40 Serial.print(setpoint);
41 Serial.print(",");

https://gist.github.com/careyi3/02a57dfd3a62a96d46171489c83488bd 23/05/2024 09 41
Page 4 sur 5
:
42 Serial.println(actual);
43

44 // Error
45 //Serial.println(error);
46
47 delay(300);
48 }
49
50 double pid(double error)
51 {
52 double proportional = error;
53 integral += error * dt;
54 double derivative = (error - previous) / dt;
55 previous = error;
56 double output = (kp * proportional) + (ki * integral) + (kd * derivative);
57 return output;
58 }

https://gist.github.com/careyi3/02a57dfd3a62a96d46171489c83488bd 23/05/2024 09 41
Page 5 sur 5
:

You might also like