Introduction
This article proposes helpful traffic lights and duty cycles Arduino Uno Project. In the meantime, we will discuss some important concepts related to these projects: PWM, duty cycles, and some functions used for these projects.
What is PWM: Pulse Width Modulation
The technique divides the carrier’s frequency into multiple levels, called “amplitudes.” Each amplitude encodes one or more bits of information. The modulated signal then has high periods of a carrier wave where there is no modulation and low periods where the amplitude changes between low and high amplitudes, encoding information.
Applicate the PWM: Pulse Width Modulation on the Arduino field
PWM constructs a square wave, a signal exchanged between on and off, which will affect the device’s energy or power consumption.There is a time limit for the signal to remain On and a time limit for the signal to remain Off. The time spent in an Off state, for example, is called a period. change period
What are Duty cycles?
Duty cycles are also crucial in determining how long a device should be inactive before being turned on again. For example, if your phone has a duty cycle of 50%, it should be inactive for 50% of the day and active for 50%.
Duty cycle Arduino application
The duty cycle is the ratio of time a device spends in an active state to the total time elapsed. The duty cycle can be expressed as a percentage or a fraction of time. If your LED has a duty cycle of 20%, it is On for 20% of the period and 80% Off.A duty cycle is the proportion of time a LED is turned On to the period of time it is turned Off.
Applied project
Components needed
- Arduino Uno board
- Breadboard
- LED
- 220-ohm resistor
- Jumper wire
- Thinkercard platform
I will use the Thinkercard simulation for this project to do the task.
The code needed for the project
Before start coding
We will use an int-type variable to store a number between 0 and 255.AnalogWrite() functionThe analogWrite() function is a special type of digitalWrite() that generates an analog voltage on a given pin. This function accepts two arguments: the specified duty cycle and a digital PWM pin.
The code of the program
// C++ code//This is a comment/* this a an other commentin multilines*/// we choose the pin ~10 from digital PWM pin.int LED=10;//We define the value of light in this variable. Light strength from 0 to 255int LedBrightness=255;void setup(){pinMode(LED,OUTPUT);}void loop(){/*If the user all the analogWrite(), the pin will render a stable rectangular wave of the specified duty cycle.*/analogWrite(LED,LedBrightness);}
Code description
Code description
When int LedBrightness equal 0, the LED is off.
When int LedBrightness equals 255, the LED is in the full glow.
When int LedBrightness equals 12, The LED is 5% glowing.
When int LedBrightness equals 25, the LED is 10% glowing.
When int LedBrightness equals 75, the LED is 30% glowing.
Project installation description
Connect the negative leg of the LED of the Arduino Uno board.Connect the positive leg of the LED the to the resistor leg of the 220-ohm resistor Connect the ~ 10 pin to the second leg of the 220-ohm resistor.Run the Thinkercard simulator.
Project development
We want to make the LED glow with a gradual increase in the intensity of the light, with a time difference of half a minute between the strength of illumination and another condition of the same illumination strength.
The code of the program
// C++ code//This a commentint LED=10;int LedBrightness=255;int waitTime = 500;void setup(){pinMode(LED,OUTPUT);}void loop(){LedBrightness = 12;analogWrite(LED, LedBrightness);delay(waitTime);LedBrightness = 24;analogWrite(LED, LedBrightness); delay(waitTime);LedBrightness = 36;analogWrite(LED, LedBrightness); delay(waitTime);LedBrightness = 48;analogWrite(LED, LedBrightness); delay(waitTime);LedBrightness = 60;analogWrite(LED, LedBrightness);delay(waitTime);LedBrightness = 72;analogWrite(LED, LedBrightness);delay(waitTime);LedBrightness = 84;analogWrite(LED, LedBrightness);delay(waitTime);LedBrightness = 96;analogWrite(LED, LedBrightness);delay(waitTime);LedBrightness = 108;analogWrite(LED, LedBrightness);delay(waitTime);LedBrightness = 120;analogWrite(LED, LedBrightness);delay(waitTime);LedBrightness = 132;analogWrite(LED, LedBrightness);delay(waitTime);LedBrightness = 144;analogWrite(LED, LedBrightness);delay(waitTime);LedBrightness = 156;analogWrite(LED, LedBrightness);delay(waitTime);LedBrightness = 168;analogWrite(LED, LedBrightness);delay(waitTime);LedBrightness = 180;analogWrite(LED, LedBrightness);delay(waitTime);LedBrightness = 192;analogWrite(LED, LedBrightness);delay(waitTime);LedBrightness = 204;analogWrite(LED, LedBrightness);delay(waitTime);LedBrightness = 216; analogWrite(LED, LedBrightness);delay(waitTime);LedBrightness = 228;analogWrite(LED, LedBrightness);delay(waitTime);LedBrightness = 255; analogWrite(LED,LedBrightness);}
Expansion concepts
Learn more bout
void setup(){ } function
The void setup() function of the Arduino programming language is used to initialize a program. It can be used to set up pins, set up serial ports, and set up LCDs.
void setup(){}
This is a function that will be executed when the Arduino board is powered on.
Void loop(){ } function
A void loop(){ } is a programming function that continuously repeats the same code.A program enters the void loop when it has no more tasks to perform. For example, when playing a game, and you have found all the items in that level, there is nothing left for the game to do but wait for you to press the next button. In this case, it would enter a void loop until you press that button.The void loop() is a programming function that is used to create an infinite loop. The code inside the function will run over and over again indefinitely.The pinMode() functionThe pinMode() function sets the operating mode of the digital input or output pin.The pinMode() function takes two parameters: the first one is a number that specifies the number of the digital I/O port, while the second parameter is a boolean value that specifies whether this port should be configured as an input or output.
delay() function
With a delay() function, you can create a pause in your program. This is useful when you want to do something else for a while before continuing.The delay() function takes one argument: the number of milliseconds to wait before executing the following line of code.
Traffic Lights Project
The Traffic Lights Project is a project that aims to teach students about how traffic lights work.The project includes a simple Arduino circuit with a traffic light and a key switch.When the key switch is turned on, the Arduino will turn on the green LED and turned off the red LED. When the key switch is turned off, it will turn off all LEDs. This project is an excellent way for beginners to learn how to take input from different sources and use that input to control something else.
Component needed for the Traffic Lights Project
- Arduino Uno board
- Breadboard
- 3 LEDs
- 3 Resistor ( 220 ohms)
- 6 jumper wires
Project installation description
Project installation description
Connect the negative leg of the green LED on the ground of the Arduino Uno board.Connect the positive leg of the green LED the to the resistor leg of the 220-ohm resistorConnect the negative leg of the yellow LED on the ground of the Arduino Uno board.Connect the positive leg of the yellow LED the to the resistor leg of the 220-ohm resistorConnect the negative leg of the red LED on the ground of the Arduino Uno board.Connect the positive leg of the red LED the to the resistor leg of the 220-ohm resistorConnect the ~ 9 pin to the second leg of the 100-ohm resistor related to the green LED.Connect the ~ 10 pin to the second leg of the 100-ohm resistor related to the yellow LED.Connect the ~ 11 pin to the second leg of the 100-ohm resistor related to the red LED.Run the Arduino Uno programm.
The code of the program
// C++ code//This a commentint greenLED=9;int yellowled=10;int redLED=11;void setup() {pinMode(greenLED,OUTPUT);pinMode(yellowled,OUTPUT);pinMode(redLED,OUTPUT);}void loop() {digitalWrite(greenLED, HIGH);delay(5000);digitalWrite(greenLED, LOW);digitalWrite(yellowled, HIGH); delay(1000);digitalWrite(yellowled, LOW); digitalWrite(redLED, HIGH); delay(5000);digitalWrite(redLED, LOW);}
Learn more bout
PinMode() function
The PinMode is a function in the Arduino programming language that is used to configure a pin for input or output.The function takes two arguments: the pin number and whether it is an input or output. For example, to configure pin 2 as an output, you would use the following:pinMode(2, OUTPUT);To configure pin 3 as an input, you would use the following:pinMode(3, INPUT);The default configuration for a pin is INPUT. A digital pin is a two-level signal with the input level, the output level, and an undefined state in between.
DigitalWrite() function
Many people think that the digitalWrite() function is only for writing code, but it can also be used for writing digitally. It’s a function that corresponds to an output pin on the Arduino board, and it controls the voltage level of an electric circuit.The digitalWrite() function is a built-in Arduino function that allows you to turn the voltage on a pin HIGH (5V) or LOW (0V) by using the following syntax:digitalWrite(pin, HIGH); //turns the voltage on pin HIGHdigitalWrite(pin, LOW); //turns the voltage on pin LOWThis function is used to control whether a circuit operates in an analog or digital mode.As an example, if we want to set pin 12 as an output, we would use digitalWrite(12, HIGH), and if we want to set it as an input, we will use digitalWrite(12, LOW).A HIGH value means that current flows through the circuit, while a LOW value represents that it does not flow.