CIPAD 23: comment contrôler 4 sorties PWM avec 4 potentiomètres

3 min read 5 hours ago
Published on Oct 02, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through the process of controlling four PWM (Pulse Width Modulation) outputs using four potentiometers with Arduino. This project is ideal for beginners looking to enhance their programming skills and apply them in practical electronics projects.

Step 1: Gather Your Components

Before you start coding, ensure you have the following components:

  • Arduino board (e.g., Arduino Uno)
  • 4 potentiometers (10kΩ recommended)
  • Breadboard
  • Jumper wires
  • LEDs (optional, for visual feedback)

Practical Tips

  • Use a breadboard for easy connections.
  • Ensure your potentiometers are rated appropriately for your project.

Step 2: Connect the Potentiometers

Next, you will connect the potentiometers to the Arduino. Follow these instructions:

  1. Connect the first terminal of each potentiometer to the 5V pin on the Arduino.
  2. Connect the second terminal (the wiper) of each potentiometer to the following analog pins:
    • Potentiometer 1 to A0
    • Potentiometer 2 to A1
    • Potentiometer 3 to A2
    • Potentiometer 4 to A3
  3. Connect the third terminal of each potentiometer to the GND pin on the Arduino.

Common Pitfalls

  • Double-check your connections to avoid short circuits.
  • Ensure that the potentiometers are functioning correctly before proceeding.

Step 3: Set Up the Arduino Code

Now it's time to write the code to read the potentiometer values and control the PWM outputs. Below is a sample code to get you started:

int pot1 = A0;
int pot2 = A1;
int pot3 = A2;
int pot4 = A3;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int val1 = analogRead(pot1);
  int val2 = analogRead(pot2);
  int val3 = analogRead(pot3);
  int val4 = analogRead(pot4);
  
  int pwm1 = map(val1, 0, 1023, 0, 255);
  int pwm2 = map(val2, 0, 1023, 0, 255);
  int pwm3 = map(val3, 0, 1023, 0, 255);
  int pwm4 = map(val4, 0, 1023, 0, 255);
  
  analogWrite(9, pwm1);
  analogWrite(10, pwm2);
  analogWrite(11, pwm3);
  analogWrite(12, pwm4);
  
  delay(100);
}

Explanation of the Code

  • analogRead: Reads the voltage from the potentiometers.
  • map: Converts the potentiometer values (0-1023) to PWM values (0-255).
  • analogWrite: Writes the PWM values to the designated pins (9 to 12).

Step 4: Upload the Code and Test

  1. Connect your Arduino to your computer using a USB cable.
  2. Open the Arduino IDE, copy the code above, and paste it into the IDE.
  3. Select your board and the correct COM port.
  4. Click on the upload button to transfer the code to the Arduino.
  5. Once uploaded, turn the potentiometers to see the PWM outputs change.

Tips for Testing

  • If you have LEDs, connect them to the PWM pins to visually see the brightness change.
  • Adjust the potentiometers smoothly to observe the effect on PWM outputs.

Conclusion

You've successfully created a project that controls four PWM outputs using potentiometers! This project serves as a great introduction to Arduino programming and electronics. As a next step, consider experimenting with different components or adding functionality, such as controlling motors or other devices. Explore further tutorials to deepen your understanding and skills in Arduino programming.