Cipad_03. Comment faire varier la luminosité d'une led. Leçon_03.
3 min read
6 days ago
Published on Oct 01, 2024
This response is partially generated with the help of AI. It may contain inaccuracies.
Table of Contents
Introduction
This tutorial guides you on how to vary the brightness of an LED using Arduino programming. It is especially useful for beginners looking to practice their skills without needing physical components. You'll learn the concepts behind PWM (Pulse Width Modulation) and how to implement it using Tinkercad, a free online simulator.
Step 1: Setting Up Tinkercad
- Go to Tinkercad: Tinkercad
- Create an account or log in if you already have one.
- Start a new project by clicking on "Create New Circuit."
- Familiarize yourself with the Tinkercad interface, including the component panel and the workspace.
Step 2: Adding Components
- Drag the following components into the workspace:
- Arduino Uno
- LED
- Resistor (220 ohms)
- Breadboard (optional for organization)
- Connect the components as follows:
- Connect the long leg (anode) of the LED to a PWM-capable pin on the Arduino (e.g., pin 9).
- Connect the short leg (cathode) of the LED to the ground (GND) through the resistor.
Step 3: Writing the Code
- Click on the "Code" button to open the code editor.
- Use the following code to control the LED brightness:
int ledPin = 9; // LED connected to pin 9
int brightness = 0; // Initial brightness level
int fadeAmount = 5; // Amount to change the brightness each time
void setup() {
pinMode(ledPin, OUTPUT); // Set pin as output
}
void loop() {
analogWrite(ledPin, brightness); // Set the brightness
brightness += fadeAmount; // Change brightness for next loop
// Reverse the direction of the fading at the ends
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount; // Reverse the direction
}
delay(30); // Wait for 30 milliseconds to see the effect
}
Step 4: Simulating the Circuit
- Click on the "Start Simulation" button to run your circuit.
- Observe how the LED gradually increases and decreases in brightness.
- Make adjustments to the
fadeAmount
ordelay
values in the code to change the fading speed.
Step 5: Experimenting with Parameters
- Try modifying the following parameters to see different effects:
- Change the
fadeAmount
to control how quickly the LED fades. - Adjust the
delay
to speed up or slow down the fading effect.
- Change the
- Experiment with different PWM-capable pins if desired.
Conclusion
In this tutorial, you learned how to set up a simple circuit to control the brightness of an LED using Arduino programming and Tinkercad. You practiced using PWM to create a fading effect and explored how to modify parameters for different results. As a next step, consider exploring more complex projects involving multiple LEDs or sensors to further enhance your skills.