Components and supplies
Arduino UNO
LM2596
Resistor 10k ohm
Geared DC Motor, 12 V
Photovoltaic 35W
SparkFun Solder-able Breadboard
LDR, 1 Mohm
Industrial Pushbutton Switch, Push-Pull
Resistor 1k ohm
Slide Switch
Dual H-Bridge motor drivers L298
Tools and machines
Soldering iron (generic)
Solder Wire, Lead Free
Apps and platforms
Arduino IDE
Project description
Code
Solar Tracker
arduino
Arduino Code. NOTE: The names of the ldrs in the code do not correspond with their place in the breadboard!
1/* Solar Tracker Code with 4 buttons for manual orientation and 2 4 ldr sensors to automaticaly traking the sun. 3 4*/ 5//set constant pin numbers 6int wbuttonPin = 4; 7int ebuttonPin = 2; 8int tbuttonPin = 8; 9int bbuttonPin = 12; 10int switch1Pin = 7; 11int switch2Pin = 5; 12int eldr = A0; 13int wldr = A1; 14int tldr = A2; 15int bldr = A3; 16int IN1 = 13; 17int IN2 = 9; 18int IN3 = 10; 19int IN4 = 11; 20int eastldr = 0; 21int westldr = 0; 22int topldr = 0; 23int botldr = 0; 24int error = 0; 25int poserror = 0; 26int tberror = 0; 27int tbposerror = 0; 28int westbutton = 0; 29int eastbutton = 0; 30int topbutton = 0; 31int botbutton = 0; 32int onoffswitch = 0; 33int singledualswitch = 0; 34void setup() { 35 pinMode (A0, INPUT); 36 pinMode (A1, INPUT); 37 pinMode (A3, INPUT); 38 pinMode (A4, INPUT); 39 pinMode (8, INPUT_PULLUP); 40 pinMode (12, INPUT_PULLUP); 41 pinMode (13, OUTPUT); 42 pinMode (9, OUTPUT); 43 pinMode (10, OUTPUT); 44 pinMode (11, OUTPUT); 45 pinMode (4, INPUT_PULLUP); 46 pinMode (2, INPUT_PULLUP); 47 pinMode (5, INPUT_PULLUP); 48 pinMode (7, INPUT_PULLUP); 49 Serial.begin(9600); 50} 51 52void loop() {Serial.print("start programm"); 53 digitalWrite(IN1, LOW); 54 digitalWrite(IN2, LOW); 55 digitalWrite(IN3, LOW); 56 digitalWrite(IN4, LOW); 57 onoffswitch = digitalRead(switch1Pin); 58 singledualswitch = digitalRead(switch2Pin); 59 while (onoffswitch == LOW ) { 60 //manual rotating of the panel 61 62 eastldr = analogRead(A0); 63 westldr = analogRead(A1); 64 topldr = analogRead(A2); 65 botldr = analogRead(A3); 66 67 eastbutton = digitalRead(ebuttonPin); 68 if (eastbutton == HIGH) 69 { digitalWrite(IN1, HIGH); 70 digitalWrite(IN2, LOW); 71 } // rotate to east side when you push the right button 72 else { 73 digitalWrite(IN1, LOW); 74 } 75 westbutton = digitalRead(wbuttonPin); 76 if (westbutton == HIGH) 77 { digitalWrite(IN2, HIGH); 78 } //rotate to west side when you push the left button 79 else { 80 digitalWrite(IN2, LOW); 81 } 82 topbutton = digitalRead(tbuttonPin); 83 if (topbutton == HIGH) 84 { digitalWrite(IN4, HIGH); 85 } //rotate to west side when you push the left button 86 else { 87 digitalWrite(IN4, LOW); 88 } 89 90 botbutton = digitalRead(bbuttonPin); 91 if (botbutton == HIGH) 92{ digitalWrite(IN3, HIGH); 93 } //rotate to west side when you push the left button 94 else { 95 digitalWrite(IN3, LOW); 96 } onoffswitch = digitalRead(switch1Pin); 97 singledualswitch = digitalRead(switch2Pin); 98 Serial.println(eastldr); 99Serial.println(westldr); 100Serial.println(topldr); 101Serial.println(botldr); 102} 103 104//automaticaly rotating of the panel 105//check east-west orientation first 106 107 108 /* if (eastldr < 400 && westldr < 400) 109 {digitalWrite(IN1,HIGH) 110 delay(7500) 111 digitalWrite(IN1,LOW)}*/ 112onoffswitch = digitalRead(switch1Pin); 113singledualswitch = digitalRead(switch2Pin); 114while (onoffswitch == HIGH && singledualswitch == LOW ){ 115 eastldr = analogRead(A0); 116 westldr = analogRead(A1); 117 topldr = analogRead(A2); 118 botldr = analogRead(A3); 119 error = (((eastldr+topldr)/2) - ((westldr+botldr)/2)); 120 poserror = abs(error); 121 if (poserror > 10){ 122 if (error > 0) { 123 digitalWrite(IN2, HIGH); 124 125// Serial.print("IN2 HIGH"); 126 } 127 else if (error < 0) { 128 digitalWrite(IN1, HIGH); 129//Serial.print("IN1 HIGH"); 130 }} 131 else if (poserror <= 10 ) { 132 digitalWrite(IN1, LOW); 133 digitalWrite(IN2, LOW); 134//Serial.print("LOW BOTH"); 135 } 136 137Serial.println(eastldr); 138Serial.println(westldr); 139Serial.println(topldr); 140Serial.println(botldr); 141 142onoffswitch = digitalRead(switch1Pin); 143singledualswitch = digitalRead(switch2Pin);} 144 145onoffswitch = digitalRead(switch1Pin); 146singledualswitch = digitalRead(switch2Pin); 147 148 149 150 while (onoffswitch == HIGH & singledualswitch == HIGH ){ 151 topldr = analogRead(A2); 152 botldr = analogRead(A3); 153 eastldr = analogRead(A0); 154 westldr = analogRead(A1); 155 156 157 tberror = (((topldr+westldr)/2) - ((botldr+eastldr)/2)); 158 tbposerror = abs(tberror); 159 160 161 error = (((eastldr+topldr)/2) - ((westldr+botldr)/2)); 162 poserror = abs(error); 163 if (poserror > 10){ 164 if (error > 0) { 165 digitalWrite(IN2, HIGH); 166 167// Serial.print("IN2 HIGH"); 168 } 169 else if (error < 0) { 170 digitalWrite(IN1, HIGH); 171 172//Serial.print("IN1 HIGH"); 173 }} 174 else if (poserror <= 10 ) { 175 digitalWrite(IN1, LOW); 176 digitalWrite(IN2, LOW); 177 } 178 179Serial.println(eastldr); 180Serial.println(westldr); 181 if (tbposerror > 10) 182{ if (tberror > 0) { 183 digitalWrite(IN4, HIGH); 184 185} else if (tberror < 0) { 186 digitalWrite(IN3, HIGH); 187 188 } 189 } 190 else if (poserror <= 10 ) { 191 digitalWrite(IN4, LOW); 192 digitalWrite(IN3, LOW); 193 } 194 onoffswitch = digitalRead(switch1Pin); 195singledualswitch = digitalRead(switch2Pin);} 196onoffswitch = digitalRead(switch1Pin); 197singledualswitch = digitalRead(switch2Pin); 198 delay(200); 199 200}
Solar Tracker
arduino
Arduino Code. NOTE: The names of the ldrs in the code do not correspond with their place in the breadboard!
1/* Solar Tracker Code with 4 buttons for manual orientation and 2 4 3 ldr sensors to automaticaly traking the sun. 4 5*/ 6//set constant pin numbers 7int 8 wbuttonPin = 4; 9int ebuttonPin = 2; 10int tbuttonPin = 8; 11int bbuttonPin 12 = 12; 13int switch1Pin = 7; 14int switch2Pin = 5; 15int eldr = A0; 16int wldr 17 = A1; 18int tldr = A2; 19int bldr = A3; 20int IN1 = 13; 21int IN2 = 9; 22int 23 IN3 = 10; 24int IN4 = 11; 25int eastldr = 0; 26int westldr = 0; 27int topldr 28 = 0; 29int botldr = 0; 30int error = 0; 31int poserror = 0; 32int tberror = 33 0; 34int tbposerror = 0; 35int westbutton = 0; 36int eastbutton = 0; 37int topbutton 38 = 0; 39int botbutton = 0; 40int onoffswitch = 0; 41int singledualswitch = 0; 42void 43 setup() { 44 pinMode (A0, INPUT); 45 pinMode (A1, INPUT); 46 pinMode (A3, 47 INPUT); 48 pinMode (A4, INPUT); 49 pinMode (8, INPUT_PULLUP); 50 pinMode (12, 51 INPUT_PULLUP); 52 pinMode (13, OUTPUT); 53 pinMode (9, OUTPUT); 54 pinMode 55 (10, OUTPUT); 56 pinMode (11, OUTPUT); 57 pinMode (4, INPUT_PULLUP); 58 pinMode 59 (2, INPUT_PULLUP); 60 pinMode (5, INPUT_PULLUP); 61 pinMode (7, INPUT_PULLUP); 62 63 Serial.begin(9600); 64} 65 66void loop() {Serial.print("start programm"); 67 68 digitalWrite(IN1, LOW); 69 digitalWrite(IN2, LOW); 70 digitalWrite(IN3, LOW); 71 72 digitalWrite(IN4, LOW); 73 onoffswitch = digitalRead(switch1Pin); 74 singledualswitch 75 = digitalRead(switch2Pin); 76 while (onoffswitch == LOW ) { 77 //manual 78 rotating of the panel 79 80 eastldr = analogRead(A0); 81 westldr 82 = analogRead(A1); 83 topldr = analogRead(A2); 84 botldr = analogRead(A3); 85 86 87 eastbutton = digitalRead(ebuttonPin); 88 if (eastbutton 89 == HIGH) 90 { digitalWrite(IN1, HIGH); 91 digitalWrite(IN2, LOW); 92 } 93 // rotate to east side when you push the right button 94 else { 95 digitalWrite(IN1, 96 LOW); 97 } 98 westbutton = digitalRead(wbuttonPin); 99 if 100 (westbutton == HIGH) 101 { digitalWrite(IN2, HIGH); 102 } //rotate to west side 103 when you push the left button 104 else { 105 digitalWrite(IN2, LOW); 106 107 } 108 topbutton = digitalRead(tbuttonPin); 109 if (topbutton 110 == HIGH) 111 { digitalWrite(IN4, HIGH); 112 } //rotate to west side when you 113 push the left button 114 else { 115 digitalWrite(IN4, LOW); 116 } 117 118 119 botbutton = digitalRead(bbuttonPin); 120 if (botbutton 121 == HIGH) 122{ digitalWrite(IN3, HIGH); 123 } //rotate to west side when you push 124 the left button 125 else { 126 digitalWrite(IN3, LOW); 127 } onoffswitch = 128 digitalRead(switch1Pin); 129 singledualswitch = digitalRead(switch2Pin); 130 131 Serial.println(eastldr); 132Serial.println(westldr); 133Serial.println(topldr); 134Serial.println(botldr); 135} 136 137 138//automaticaly rotating of the panel 139//check east-west orientation first 140 141 142 143 /* if (eastldr < 400 && westldr < 400) 144 145 {digitalWrite(IN1,HIGH) 146 delay(7500) 147 148 digitalWrite(IN1,LOW)}*/ 149onoffswitch 150 = digitalRead(switch1Pin); 151singledualswitch = digitalRead(switch2Pin); 152while 153 (onoffswitch == HIGH && singledualswitch == LOW ){ 154 eastldr = analogRead(A0); 155 156 westldr = analogRead(A1); 157 topldr = analogRead(A2); 158 botldr 159 = analogRead(A3); 160 error = (((eastldr+topldr)/2) - ((westldr+botldr)/2)); 161 162 poserror = abs(error); 163 if 164 (poserror > 10){ 165 if (error > 0) { 166 digitalWrite(IN2, HIGH); 167 168// Serial.print("IN2 169 HIGH"); 170 } 171 else if (error < 0) { 172 digitalWrite(IN1, HIGH); 173//Serial.print("IN1 174 HIGH"); 175 }} 176 else if (poserror <= 10 ) { 177 digitalWrite(IN1, LOW); 178 179 digitalWrite(IN2, LOW); 180//Serial.print("LOW BOTH"); 181 } 182 183Serial.println(eastldr); 184Serial.println(westldr); 185Serial.println(topldr); 186Serial.println(botldr); 187 188onoffswitch 189 = digitalRead(switch1Pin); 190singledualswitch = digitalRead(switch2Pin);} 191 192onoffswitch 193 = digitalRead(switch1Pin); 194singledualswitch = digitalRead(switch2Pin); 195 196 197 198 199 while (onoffswitch == HIGH & singledualswitch == HIGH ){ 200 topldr = 201 analogRead(A2); 202 botldr = analogRead(A3); 203 eastldr = analogRead(A0); 204 205 westldr = analogRead(A1); 206 207 208 209 tberror = (((topldr+westldr)/2) - ((botldr+eastldr)/2)); 210 tbposerror 211 = abs(tberror); 212 213 214 error = (((eastldr+topldr)/2) 215 - ((westldr+botldr)/2)); 216 poserror = abs(error); 217 218 if (poserror > 10){ 219 if (error > 0) { 220 221 digitalWrite(IN2, HIGH); 222 223// Serial.print("IN2 HIGH"); 224 } 225 else 226 if (error < 0) { 227 digitalWrite(IN1, HIGH); 228 229//Serial.print("IN1 HIGH"); 230 231 }} 232 else if (poserror <= 10 ) { 233 digitalWrite(IN1, LOW); 234 digitalWrite(IN2, 235 LOW); 236 } 237 238Serial.println(eastldr); 239Serial.println(westldr); 240 if 241 (tbposerror > 10) 242{ if (tberror > 0) { 243 digitalWrite(IN4, HIGH); 244 245} 246 else if (tberror < 0) { 247 digitalWrite(IN3, HIGH); 248 249 } 250 251 } 252 else if (poserror <= 10 ) { 253 digitalWrite(IN4, LOW); 254 digitalWrite(IN3, 255 LOW); 256 } 257 onoffswitch = digitalRead(switch1Pin); 258singledualswitch = digitalRead(switch2Pin);} 259onoffswitch 260 = digitalRead(switch1Pin); 261singledualswitch = digitalRead(switch2Pin); 262 delay(200); 263 264}
Downloadable files
Circuit
LM2596 and L298 shown in the picture Download the other schematic for more clear vision of Arduino pins
Circuit
Circuit
You can use 1 breadboard instead of two. I just put second breadboard for the 4 LDRs above the Photovoltaic which is far from the first breadboard. Download this schematic for more clear vision of Arduino pins
Circuit
Circuit
LM2596 and L298 shown in the picture Download the other schematic for more clear vision of Arduino pins
Circuit
Comments
Only logged in users can leave comments