Skip to content

Manual and Automatic Traffic Lights

Now... let's start!

LEDs light faded to on or off, through the pot, using PWM (Pulse Width Modulation).

"oh oh! sorry, it's late; my dog just called me on phone, have to bring house to piss, my wife's unattended..."

Stop! We'll use PWM without knowing anything about it.

Just learn that it's a technique to output analog-continuous values with digital-discontinuous ones.

If you mounted the circuit as in first picture, disconnect every wire from Arduino; now connect it only to pc.

 

 

 

Open Arduino IDE, tool to program it; write, compile the following code and upload to the board.

const int analogInPin = A0;  // Analog input pin that the potentiometer (wiper)
                             // is attached to

const int analogOutPin9 = 9;   // Check your arduino board
const int analogOutPin10 = 10; // among digital pins
const int analogOutPin11 = 11; // you'll find someone marked with PWM

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  // every command starting with "Serial" means a communication with a serial
  // monitor, if arduino is connected to pc you can use the internal one
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  // Arduino can generate PWM on a scale from 0 to 255; so that map function
  // returns value proportional to the input, that goes from 0 to 1023

 analogWrite(analogOutPin9, outputValue);
 Serial.print("sensor = " );
 Serial.print(sensorValue);
 Serial.print("\t output 9 = ");
 Serial.println(outputValue);

 analogWrite(analogOutPin10, outputValue);
 Serial.print("sensor = " );
 Serial.print(sensorValue);
 Serial.print("\t output 10 = ");
 Serial.println(outputValue);

 analogWrite(analogOutPin11, outputValue);
 Serial.print("sensor = " );
 Serial.print(sensorValue);
 Serial.print("\t output 11 = ");
 Serial.println(outputValue); 

 delay(3); // 3 milliseconds every loop
}

We now can fade LEDs all together. Watch the video below.