CIPAD 21 Part 1: Commutation à bascule numérique logique Marche/Arrêt (On/Off)

3 min read 3 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 guides you through creating a digital toggle switch (On/Off) using Arduino programming. It is based on the CIPAD course aimed at beginners, particularly focusing on the logic behind a digital toggle switch. You'll learn how to write the necessary code and set up the hardware components effectively.

Step 1: Gather Required Components

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

  • Arduino board (e.g., Arduino Uno)
  • Push button switch
  • Resistor (10k ohm)
  • LED
  • Breadboard
  • Jumper wires

Practical Tips

  • Organize your workspace to easily access components.
  • Familiarize yourself with the Arduino IDE for uploading your code.

Step 2: Set Up the Circuit

Follow these steps to build the circuit on your breadboard:

  1. Connect the Push Button:

    • Place the push button on the breadboard.
    • Connect one terminal of the button to a digital pin on the Arduino (e.g., pin 2).
    • Connect the other terminal to the ground (GND).
  2. Add the Resistor:

    • Connect a 10k ohm resistor from the digital pin (where the button connects) to the ground. This acts as a pull-down resistor.
  3. Connect the LED:

    • Insert the LED into the breadboard.
    • Connect the longer leg (anode) to a digital pin on the Arduino (e.g., pin 13).
    • Connect the shorter leg (cathode) to the ground.

Common Pitfalls to Avoid

  • Ensure all connections are secure; loose wires can cause your circuit to malfunction.
  • Double-check the orientation of the LED to prevent damage.

Step 3: Write the Arduino Code

Open the Arduino IDE and input the following code:

int buttonPin = 2; // Pin number for the button
int ledPin = 13;   // Pin number for the LED
int buttonState = 0; // Variable for reading the button status
int lastButtonState = 0; // Variable to hold the last button state
bool ledState = false; // Variable to keep track of the LED state

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  
  // Check if the button is pressed
  if (buttonState == HIGH && lastButtonState == LOW) {
    ledState = !ledState; // Toggle the LED state
    digitalWrite(ledPin, ledState); // Set the LED
    delay(50); // Delay for debounce
  }
  
  lastButtonState = buttonState; // Store the current button state
}

Explanation of Code

  • The code initializes pins for the button and LED.
  • In the loop(), it checks for button presses and toggles the LED state accordingly.

Step 4: Upload the Code to Arduino

  1. Connect your Arduino board to your computer using a USB cable.
  2. Select the correct board type and port from the Arduino IDE.
  3. Click on the upload button to transfer the code to the Arduino.

Practical Tips

  • Monitor the IDE for any error messages; these can help troubleshoot issues with the code.

Step 5: Test Your Circuit

After uploading the code, test your circuit by pressing the button:

  • The LED should turn on when the button is pressed and toggle off when pressed again.
  • If the LED does not respond, check your connections and code for errors.

Conclusion

In this tutorial, you learned how to create a digital toggle switch using Arduino. You gathered components, built the circuit, wrote the code, and tested your setup. For further learning, consider experimenting with different components or expanding this project by adding more LEDs or features. Happy coding!