In our earlier article, We have discussed PWM and informed you that this PWD technology is used in particular for light-emitting diodes (LEDs), as they are often used as backlights on mobile phones or brake lights in newer motor vehicles.
Our older article on Breathing LED also uses PWM. This was the sketch, LED is attached to pin 11:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | // Distributed under GNU GPL 3.0 int i = 0; void setup() { for(i = 0 ; i <= 15; i+=1) { analogWrite(11, i); delay(5); } } void loop() { for(i = 15 ; i <= 255; i+=1) { analogWrite(11, i); if (i > 150) { delay(4); } if ((i > 125) && (i < 151)) { delay(5); } if (( i > 100) && (i < 126)) { delay(7); } if (( i > 75) && (i < 101)) { delay(10); } if (( i > 50) && (i < 76)) { delay(14); } if (( i > 25) && (i < 51)) { delay(18); } if (( i > 1) && (i < 26)) { delay(19); } } for(i = 255; i >=15; i-=1) { analogWrite(11, i); if (i > 150) { delay(4); } if ((i > 125) && (i < 151)) { delay(5); } if (( i > 100) && (i < 126)) { delay(7); } if (( i > 75) && (i < 101)) { delay(10); } if (( i > 50) && (i < 76)) { delay(14); } if (( i > 25) && (i < 51)) { delay(18); } if (( i > 1) && (i < 26)) { delay(19); } } delay(970); } |
This article is about creating an LED chaser using PWM. This code is using simple logic (it is not PWM):
---
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | int LED13 = 13; int LED12 = 12; int LED11 = 11; int LED10 = 10; int LED9 = 9; int LED8 = 8; int LED7 = 7; int LED6 = 6; int LED5 = 5; int LED4 = 4; int LED3 = 3; int LED2 = 2; void setup() { pinMode(LED2, OUTPUT); pinMode(LED3, OUTPUT); pinMode(LED4, OUTPUT); pinMode(LED5, OUTPUT); pinMode(LED6, OUTPUT); pinMode(LED7, OUTPUT); pinMode(LED8, OUTPUT); pinMode(LED9, OUTPUT); pinMode(LED10, OUTPUT); pinMode(LED11, OUTPUT); pinMode(LED12, OUTPUT); pinMode(LED13, OUTPUT); } void loop() { digitalWrite(LED2, HIGH); delay(100); digitalWrite(LED2, LOW); delay(100); digitalWrite(LED3, HIGH); delay(100); digitalWrite(LED3, LOW); delay(100); digitalWrite(LED4, HIGH); delay(100); digitalWrite(LED4, LOW); delay(100); digitalWrite(LED5, HIGH); delay(100); digitalWrite(LED5, LOW); delay(100); digitalWrite(LED6, HIGH); delay(100); digitalWrite(LED6, LOW); delay(100); digitalWrite(LED7, HIGH); delay(100); digitalWrite(LED7, LOW); delay(100); digitalWrite(LED8, HIGH); delay(100); digitalWrite(LED8, LOW); delay(100); digitalWrite(LED9, HIGH); delay(100); digitalWrite(LED9, LOW); delay(100); digitalWrite(LED10, HIGH); delay(100); digitalWrite(LED10, LOW); delay(100); digitalWrite(LED11, HIGH); delay(100); digitalWrite(LED11, LOW); delay(100); digitalWrite(LED12, HIGH); delay(100); digitalWrite(LED12, LOW); delay(100); digitalWrite(LED13, HIGH); delay(100); digitalWrite(LED13, LOW); delay(100); } |
This is a chaser using PWM. Click to simulate and view:
Below will be the circuit. It is easy but add a 220 ohm resistor to each LED. I created the thing in a hurry to test whether the code worked.
This is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | int ledPin1 = 2; int ledPin2 = 3; int ledPin3 = 4; int ledPin4 = 5; int ledPin5 = 6; int ledPin6 = 7; int ledPin7 = 8; int ledPin8 = 9; int ledPin9 = 10; int ledPin10 = 11; int ledPin11 = 12; int ledPin12 = 13; void setup() { pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); pinMode(ledPin4, OUTPUT); pinMode(ledPin5, OUTPUT); pinMode(ledPin6, OUTPUT); pinMode(ledPin7, OUTPUT); pinMode(ledPin8, OUTPUT); pinMode(ledPin9, OUTPUT); pinMode(ledPin10, OUTPUT); pinMode(ledPin11, OUTPUT); pinMode(ledPin12, OUTPUT); } void loop() { // Fade in for(int ledVal = 0; ledVal <= 255; ledVal +=1) { analogWrite(ledPin1, ledVal); delay(0); } // Fade out for(int ledVal = 255; ledVal >= 0; ledVal -=1) { analogWrite(ledPin1, ledVal); delay(0); } // Pause for 0/1000 second delay(0); // Fade in for(int ledVal = 0; ledVal <= 255; ledVal +=1) { analogWrite(ledPin2, ledVal); delay(0); } // Fade out for(int ledVal = 255; ledVal >= 0; ledVal -=1) { analogWrite(ledPin2, ledVal); delay(0); } // Pause for 0/1000 second delay(0); // Fade in for(int ledVal = 0; ledVal <= 255; ledVal +=1) { analogWrite(ledPin3, ledVal); delay(0); } // Fade out for(int ledVal = 255; ledVal >= 0; ledVal -=1) { analogWrite(ledPin3, ledVal); delay(0); } // Pause for 0/1000 second delay(0); // Fade in for(int ledVal = 0; ledVal <= 255; ledVal +=1) { analogWrite(ledPin4, ledVal); delay(0); } // Fade out for(int ledVal = 255; ledVal >= 0; ledVal -=1) { analogWrite(ledPin4, ledVal); delay(0); } // Pause for 0/1000 second delay(0); // Fade in for(int ledVal = 0; ledVal <= 255; ledVal +=1) { analogWrite(ledPin5, ledVal); delay(0); } // Fade out for(int ledVal = 255; ledVal >= 0; ledVal -=1) { analogWrite(ledPin5, ledVal); delay(0); } // Pause for 0/1000 second delay(0); // Fade in for(int ledVal = 0; ledVal <= 255; ledVal +=1) { analogWrite(ledPin6, ledVal); delay(0); } // Fade out for(int ledVal = 255; ledVal >= 0; ledVal -=1) { analogWrite(ledPin6, ledVal); delay(0); } // Pause for 0/1000 second delay(0); // Fade in for(int ledVal = 0; ledVal <= 255; ledVal +=1) { analogWrite(ledPin7, ledVal); delay(0); } // Fade out for(int ledVal = 255; ledVal >= 0; ledVal -=1) { analogWrite(ledPin7, ledVal); delay(0); } // Pause for 0/1000 second delay(0); // Fade in for(int ledVal = 0; ledVal <= 255; ledVal +=1) { analogWrite(ledPin8, ledVal); delay(0); } // Fade out for(int ledVal = 255; ledVal >= 0; ledVal -=1) { analogWrite(ledPin8, ledVal); delay(0); } // Pause for 0/1000 second delay(0); // Fade in for(int ledVal = 0; ledVal <= 255; ledVal +=1) { analogWrite(ledPin9, ledVal); delay(0); } // Fade out for(int ledVal = 255; ledVal >= 0; ledVal -=1) { analogWrite(ledPin9, ledVal); delay(0); } // Pause for 0/1000 second delay(0); // Fade in for(int ledVal = 0; ledVal <= 255; ledVal +=1) { analogWrite(ledPin10, ledVal); delay(0); } // Fade out for(int ledVal = 255; ledVal >= 0; ledVal -=1) { analogWrite(ledPin10, ledVal); delay(0); } // Pause for 0/1000 second delay(0); // Fade in for(int ledVal = 0; ledVal <= 255; ledVal +=1) { analogWrite(ledPin11, ledVal); delay(0); } // Fade out for(int ledVal = 255; ledVal >= 0; ledVal -=1) { analogWrite(ledPin11, ledVal); delay(0); } // Pause for 0/1000 second delay(0); // Fade in for(int ledVal = 0; ledVal <= 255; ledVal +=1) { analogWrite(ledPin12, ledVal); delay(0); } // Fade out for(int ledVal = 255; ledVal >= 0; ledVal -=1) { analogWrite(ledPin12, ledVal); delay(0); } // Pause for 500/1000 second delay(500); } |
Here is the GitHub repository with the above sketches.
Here is the TinkerCAD URL of the thing.