Components and supplies
Arduino UNO
LED (generic)
Rotary potentiometer (generic)
DC Motor, Miniature
Alphanumeric LCD, 16 x 2
Machine Screw, M3
LDR, 5 Mohm
Tools and machines
3D Printer (generic)
Apps and platforms
Arduino IDE
Project description
Code
PID Controlled DC motor
c_cpp
1//////////PID/////////// 2#include <PID_v1.h> 3 4//////////Display/////////// 5#include <LiquidCrystal_I2C.h> 6 7//////////PID/////////// 8//Define variable 9double Setpoint = 0, Input = 0, Output = 0; 10//Define the PID named myPID 11PID myPID(&Input, &Output, &Setpoint, 1, 1, 0, DIRECT); 12 13//////////Display/////////// 14LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display 15unsigned long t3 = 0; 16unsigned long update_time = 200; 17 18/////Sensor//// 19int LDR_Pin = A0; 20unsigned long t1, t2; 21unsigned long braketime = 300000; 22unsigned long dt = 0; 23unsigned long rpm = 0; 24int wait_time = 2; 25 26 27/////Driver//// 28//channels 29const int pwm = 9; 30const int in_1 = 5; 31const int in_2 = 6; 32const int led_pin = 13; 33 34//////Poti///// 35int Pot_pin = A2; 36 37void setup() { 38 39 lcd.init(); // initialize the lcd 40 lcd.backlight(); 41 42 myPID.SetMode(AUTOMATIC); //turn the PID on 43 44 Serial.begin(9600); 45 46 pinMode(pwm, OUTPUT); //define channels 47 pinMode(in_1, OUTPUT); 48 pinMode(in_2, OUTPUT); 49 pinMode(led_pin, OUTPUT); 50 51 digitalWrite(led_pin, HIGH); //turn led on 52} 53 54void loop() { 55 56 //Define the Setpoint 57 //Setpoint = (sin(3.0 * millis() / (1000.0 * 2 * 3.14)) + 2.0 ) * 500.0; 58 //Setpoint = 0; 59 Setpoint = analogRead(Pot_pin) * 5.0; 60 61 ////////measure and calculate rpm//////// 62 ///////////////////////////////////////// 63 //detecting the time dt which passes between two holes 64 65 //1. starting at an unknown state and wait until the photoresistor indicates that the light barrier is blocked 66 t2 = micros(); //t2 indicates the starting time of the loop 67 while (analogRead(LDR_Pin) < 850) { //The loop runs until the LDR signal is lower than the threshold of 850 68 if (micros() - t2 > braketime) { //If the loop runs longer than braketime e.g. the rotor did not move the loop stopped 69 break; 70 } 71 } 72 delay(wait_time); //this delay time is needed due to the noise in the signal 73 //2. wait until the next hole appears and save this time to the variable t1 74 t2 = micros(); 75 while (analogRead(LDR_Pin) > 850) { //The loop runs until the LDR signal is higher than the threshold of 850 76 if (micros() - t2 > braketime) { 77 break; 78 } 79 } 80 t1 = micros(); //save the time when the first hole appear to t1 81 delay(wait_time); //this delay time is needed due to the noise in the signal 82 //3. wait until the hole disappears 83 t2 = micros(); 84 while (analogRead(LDR_Pin) < 850) { //The loop runs until the LDR signal is lower than the trashhold of 850 85 if (micros() - t2 > braketime) { 86 break; 87 } 88 } 89 delay(wait_time); //this delay time is needed due to the noise in the signal 90 91 //4.wait until the next hole appears 92 t2 = micros(); 93 while (analogRead(LDR_Pin) > 850) { //The loop runs until the LDR signal is higher than the trashhold of 850 94 if (micros() - t2 > braketime) { 95 break; 96 } 97 } 98 dt = micros() - t1; //calculate the time between the two holes 99 delay(wait_time); 100 rpm = (1.0 / (dt / 60000000.0)) / 2.0; //calculate the rpm 101 102 //use the PID controller 103 Input = rpm; 104 myPID.Compute(); 105 digitalWrite(in_1, HIGH); //Defining the turning direktion 106 digitalWrite(in_2, LOW); 107 analogWrite(pwm, Output); //apply the Output to the DC motor 108 109 Serial.print(rpm - Setpoint); 110 Serial.print(","); 111 Serial.print(rpm); 112 Serial.print(","); 113 Serial.print(Setpoint); 114 Serial.print(","); 115 Serial.println(Output); 116 117 //show results at the LCD 118 if (millis() - t3 > update_time) 119 { 120 t3 = millis(); 121 lcd.clear(); 122 lcd.setCursor(0, 0); 123 lcd.print("set rpm"); 124 lcd.setCursor(8, 0); 125 lcd.print(int(Setpoint)); 126 lcd.setCursor(13, 0); 127 lcd.print("U/m"); 128 lcd.setCursor(0, 1); 129 lcd.print("act rpm"); 130 lcd.setCursor(8, 1); 131 lcd.print(int(rpm)); 132 lcd.setCursor(13, 1); 133 lcd.print("U/m"); 134 } 135}
PID Controlled DC motor
c_cpp
1//////////PID/////////// 2#include <PID_v1.h> 3 4//////////Display/////////// 5#include 6 <LiquidCrystal_I2C.h> 7 8//////////PID/////////// 9//Define variable 10double 11 Setpoint = 0, Input = 0, Output = 0; 12//Define the PID named myPID 13PID myPID(&Input, 14 &Output, &Setpoint, 1, 1, 0, DIRECT); 15 16//////////Display/////////// 17LiquidCrystal_I2C 18 lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display 19unsigned 20 long t3 = 0; 21unsigned long update_time = 200; 22 23/////Sensor//// 24int LDR_Pin 25 = A0; 26unsigned long t1, t2; 27unsigned long braketime = 300000; 28unsigned 29 long dt = 0; 30unsigned long rpm = 0; 31int wait_time = 2; 32 33 34/////Driver//// 35//channels 36const 37 int pwm = 9; 38const int in_1 = 5; 39const int in_2 = 6; 40const int led_pin 41 = 13; 42 43//////Poti///// 44int Pot_pin = A2; 45 46void setup() { 47 48 49 lcd.init(); // initialize the lcd 50 lcd.backlight(); 51 52 53 myPID.SetMode(AUTOMATIC); //turn the PID on 54 55 Serial.begin(9600); 56 57 58 pinMode(pwm, OUTPUT); //define channels 59 pinMode(in_1, OUTPUT); 60 61 pinMode(in_2, OUTPUT); 62 pinMode(led_pin, OUTPUT); 63 64 digitalWrite(led_pin, 65 HIGH); //turn led on 66} 67 68void loop() { 69 70 //Define the Setpoint 71 72 //Setpoint = (sin(3.0 * millis() / (1000.0 * 2 * 3.14)) + 2.0 ) * 500.0; 73 //Setpoint 74 = 0; 75 Setpoint = analogRead(Pot_pin) * 5.0; 76 77 ////////measure and calculate 78 rpm//////// 79 ///////////////////////////////////////// 80 //detecting the 81 time dt which passes between two holes 82 83 //1. starting at an unknown state 84 and wait until the photoresistor indicates that the light barrier is blocked 85 86 t2 = micros(); //t2 indicates the starting time of the 87 loop 88 while (analogRead(LDR_Pin) < 850) { //The loop runs until the LDR 89 signal is lower than the threshold of 850 90 if (micros() - t2 > braketime) 91 { //If the loop runs longer than braketime e.g. the rotor did not move the 92 loop stopped 93 break; 94 } 95 } 96 delay(wait_time); //this 97 delay time is needed due to the noise in the signal 98 //2. wait until the next 99 hole appears and save this time to the variable t1 100 t2 = micros(); 101 102 while (analogRead(LDR_Pin) > 850) { //The loop runs until the LDR signal is 103 higher than the threshold of 850 104 if (micros() - t2 > braketime) { 105 106 break; 107 } 108 } 109 t1 = micros(); //save 110 the time when the first hole appear to t1 111 delay(wait_time); //this 112 delay time is needed due to the noise in the signal 113 //3. wait until the hole 114 disappears 115 t2 = micros(); 116 while (analogRead(LDR_Pin) 117 < 850) { //The loop runs until the LDR signal is lower than the trashhold of 118 850 119 if (micros() - t2 > braketime) { 120 break; 121 } 122 } 123 124 delay(wait_time); //this delay time is needed due to the 125 noise in the signal 126 127 //4.wait until the next hole appears 128 t2 = micros(); 129 130 while (analogRead(LDR_Pin) > 850) { //The loop 131 runs until the LDR signal is higher than the trashhold of 850 132 if (micros() 133 - t2 > braketime) { 134 break; 135 } 136 } 137 dt = micros() - t1; //calculate 138 the time between the two holes 139 delay(wait_time); 140 rpm = (1.0 / 141 (dt / 60000000.0)) / 2.0; //calculate the rpm 142 143 //use the PID controller 144 145 Input = rpm; 146 myPID.Compute(); 147 digitalWrite(in_1, HIGH); //Defining 148 the turning direktion 149 digitalWrite(in_2, LOW); 150 analogWrite(pwm, Output); 151 //apply the Output to the DC motor 152 153 Serial.print(rpm - Setpoint); 154 155 Serial.print(","); 156 Serial.print(rpm); 157 Serial.print(","); 158 159 Serial.print(Setpoint); 160 Serial.print(","); 161 Serial.println(Output); 162 163 164 //show results at the LCD 165 if (millis() - t3 > update_time) 166 { 167 t3 168 = millis(); 169 lcd.clear(); 170 lcd.setCursor(0, 0); 171 lcd.print("set 172 rpm"); 173 lcd.setCursor(8, 0); 174 lcd.print(int(Setpoint)); 175 lcd.setCursor(13, 176 0); 177 lcd.print("U/m"); 178 lcd.setCursor(0, 1); 179 lcd.print("act 180 rpm"); 181 lcd.setCursor(8, 1); 182 lcd.print(int(rpm)); 183 lcd.setCursor(13, 184 1); 185 lcd.print("U/m"); 186 } 187}
Downloadable files
wiring
All
wiring
Comments
Only logged in users can leave comments
NG-Design
0 Followers
•0 Projects
Table of contents
Intro
6
0