CIPAD 49: initiation à Wokwi, la fonction tone(), les boutons poussoirs, le buzzer

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

Table of Contents

Introduction

In this tutorial, we will explore the basics of using Wokwi, an online Arduino simulator. We will learn how to utilize the tone() function, work with push buttons, and integrate a buzzer into our projects. This guide is suitable for beginners looking to enhance their programming skills with Arduino while working on a practical project.

Step 1: Setting Up Wokwi

  • Navigate to the Wokwi website and create a new project.
  • Familiarize yourself with the interface:
    • Components Panel: Here, you can find various electronic components to use in your project.
    • Code Editor: This is where you will write your Arduino code.
  • To download the necessary library for sound pitches, visit this link and add it to your project.

Step 2: Adding Components

  • In the Components Panel, search for and add the following components:
    • Buzzer: This will produce sound.
    • Push Button: This will be used to trigger the buzzer.
    • Breadboard: To organize your circuit.
  • Connect the components:
    • Connect one terminal of the push button to the ground (GND).
    • Connect the other terminal to a digital pin on the Arduino (e.g., pin 2).
    • Connect the buzzer’s positive terminal to another digital pin (e.g., pin 8) and its negative terminal to GND.

Step 3: Writing the Code

  • Open the code editor and start writing your Arduino sketch. Below is a basic example to get you started:
#include "pitches.h"

const int buttonPin = 2; // Pin where the push button is connected
const int buzzerPin = 8; // Pin where the buzzer is connected
int buttonState = 0; // Variable to store the button state

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

void loop() {
  buttonState = digitalRead(buttonPin); // Read the state of the push button
  if (buttonState == HIGH) {
    tone(buzzerPin, 440); // Play sound at 440 Hz
  } else {
    noTone(buzzerPin); // Stop sound
  }
}
  • This code sets up the buzzer to play a tone when the button is pressed.

Step 4: Testing Your Project

  • Click on the "Start Simulation" button in Wokwi.
  • Press the button in the simulation and listen for the buzzer to sound.
  • Ensure that the buzzer stops when you release the button.

Step 5: Troubleshooting Common Issues

  • If the buzzer does not sound:
    • Check your wiring connections.
    • Ensure the correct pins are set in the code.
    • Verify that the button is functioning properly.

Conclusion

In this tutorial, we covered the basics of using Wokwi to simulate an Arduino project with a buzzer and push button. You learned how to set up the environment, add components, write a simple code to play sounds, and troubleshoot common issues. As a next step, consider experimenting with different tones by modifying the frequency in the tone() function or adding more buttons to create a more complex project. Happy coding!