Introduction: Arduino Nano: Debouncing, and Toggle Button With Visuino

Buttons connected to Arduino can easily be used as On/Off Toggle Switch. Unfortunately the buttons tend to create bouncing noise when they connect/disconnect, making the toggle functionality unreliable. The noise can be suppressed with a technique called Debouncing. In this Instructable I will show you how you can implement a Toggle switch with Arduino, and how you can add Debouncing to reduce switching noise. This will be done with the help of Visuino, an easy to use graphical development environment for Arduino.

Step 1: Components

  1. One Arduino compatible board (I use Arduino Nano, because I have one, but any other will be just fine)
  2. One Button Sensor module I got from this cheap 37 sensors set.
  3. 3 Female-Female jumper wires

Step 2: Connect the Button to Arduino

  1. Connect Ground(Black wire), Power(Red wire), and Data(Gray wire) to the Button Module (Picture 1)
  2. Connect the other end of the Power wire(Red wire) to the 5V power pin of the Arduino board(Picture 2)
  3. Connect the other end of the Ground wire(Black wire) to Ground pin of the Arduino board(Picture 2)
  4. Connect the other end of the Data wire(Gray wire) to Digital pin 2 of the Arduino board(Picture 3, and 4)
  5. Picture 5 shows where are the Ground, 5V Power, and Digital 2 pins of the Arduino Nano.

Step 3: Start Visuino, and Select the Arduino Board Type

To start programming the Arduino, you will need to have the Arduino IDE installed from here: http://www.arduino.cc/ .

Make sure that you install 1.6.7 or higher, otherwise this Instructable will not work!

The Visuino: https://www.visuino.com also needs to be installed.

  1. Start Visuino as shown in the first picture
  2. Click on the "Tools" button on the Arduino component (Picture 1) in Visuino,
  3. When the dialog appears, select Arduino Nano as shown in Picture 2

Step 4: In Visuino: Add Component to Detect the Level Change Edge When Pressing the Button

First we need to generate event only when the button is pressed. For this we need an edge detector, to detect the change of the digital level from False to True (or optionally from True to False).

  1. Type "edge" in the Filter box of the Component Toolbox then select the "Detect Edge" component (Picture 1), and drop it in the design area
  2. Connect the "Out" pin of the "Digital[ 2 ]" channel of the Arduino component to the "In" pin of the DetectEdge1 (Picture 2)
  3. Optionally: If your Button is Inverted (Sends False when pressed), set the value of the "Raising" property to False (Picture 3)

Step 5: In Visuino: Add a Toggle(T)-Flip Flop to Implement the Toggle

To implement the Toggle functionality, we need to use a Flip-Flop. The most convenient for this purpose is the Toggle(T)-FlipFlop.

  1. Type "flip" in the Filter box of the Component Toolbox then select the "Toggle(T) Flip-Flop" component (Picture 1), and drop it in the design area
  2. Connect the "Out" pin of the DetectEdge1 to the "In" pin of the Flip-Flop(Picture 2)
  3. Connect the "Out" pin of the TFlipFlop1component (Picture 3) to the "Digital" input pin of the Digital[ 13 ] channel of the Arduino component (Picture 4)

Step 6: Generate, Compile, and Upload the Arduino Code

  1. In Visuino, Press F9 or click on the button shown on Picture 1 to generate the Arduino code, and open the Arduino IDE
  2. In the Arduino IDE, click on the Upload button, to compile and upload the code (Picture 2)

Step 7: And Play...

On Picture 1 you can see the complete Visuino diagram.

If you run the project however, from time to time you will see that when you press the button once, the LED changes state more than once :-( . The problem is that there is a noise - bouncing of the signal coming from the button when the button switches. The signal will go up and down multiple times before it settles. You can see the problem shown in Oscilloscope on Picture 2.

To fix this problem we need to use technique called Debouncing. Visuino includes Debouncing component, and makes implementing this very easy.

Step 8: In Visuino: Add Debounce Button Component

  1. Type "but" in the Filter box of the Component Toolbox then select the "Debounce Button" component (Picture 1), and drop it in the design area
  2. If necessary make some space for it between the Arduino board and the DetectEdge1, so we can have a cleaner diagram(Picture 2)
  3. Connect the "Out" pin of the Digital[ 2 ] channel of the Arduino component to the "In" pin of the Button1 component(Picture 3)
  4. Connect the "Out" pin of the Button1 component to the "In" pin of the DetectEdge1 component(Picture 4)

Step 9:

  1. In Visuino, Press F9 or click on the button shown on Picture 1 to generate the Arduino code, and open the Arduino IDE
  2. In the Arduino IDE, click on the Upload button, to compile and upload the code (Picture 2)

Step 10: And Play...

On Picture 1 you can see the complete Visuino diagram.

If you start the Arduino, every time you click on the button, the LED on Pin 13 will change its state - If it was ON it will turn OFF, and if it was OFF will turn ON as you can see from Pictures 2, 3, 4, and 5, and in the video.

Congratulations! You have implemented a noise resistant Arduino Toggle switch, by using Debouncing.

Also attached in the ZIP are the two Visuino projects, that I created for this Instructable. You can download and open them in Visuino: https://www.visuino.com

Comments

About This Instructable

3,645views

14favorites

License:

More by BoianM:Program Arduino UNO With Visuino to Draw on ILI9341 TFT Touchscreen Display Shield With PenAssemble Kuman Wi-Fi Robot With Camera and ManipulatorArduino and Visuino: Long Distance Remote Light Sensor With RFM95W/RFM98W Makerfabs LoRa Shields
Add instructable to: