Arduino Nano: Debouncing, and Toggle Button With Visuino

by BoianM in Circuits > Arduino

9446 Views, 16 Favorites, 0 Comments

Arduino Nano: Debouncing, and Toggle Button With Visuino

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.

Components

5676e65b50e1b66891000a7a.jpeg
  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

Connect the Button to Arduino

5676e62e2e7fb65b2f000e92.jpeg
56770c8767400c93e90001be.jpeg
5676e6b645bcebb0760000a8.jpeg
5676e6da67400ccf020005b8.jpeg
56770c592e7fb65b2f000f50.jpeg
  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.

Start Visuino, and Select the Arduino Board Type

SelectBoard.png
SelectBoard.png

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

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

ComponentEdge.png
ComponentEdgeConect.png
ComponentEdgeProperty.png

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)

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

ComponentFlipFlop.png
ComponentFlipFlopConnect.png
ComponentFlipFlopConnect2.png
ComponentFlipFlopConnect3.png

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 TFlipFlop1 component (Picture 3) to the "Digital" input pin of the Digital[ 13 ] channel of the Arduino component (Picture 4)

Generate, Compile, and Upload the Arduino Code

GenerateCtopped.png
ArduinoIDE1.png
  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)

And Play...

ButtonDiagramCropped.png
Bouncy_Switch.png

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.

In Visuino: Add Debounce Button Component

ComponentButton.png
ComponentButtonAdd.png
ComponentButtonConnect1.png
ComponentButtonConnect2.png
  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)

GenerateCtopped.png
ArduinoIDE2.png
  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)

And Play...

DebouncedButtonDiagramCropped.png
58128ed945bceb8494001678.jpeg
58128ef050e1b69c73001199.jpeg
58128f0f937ddbb768000c8b.jpeg
58128f2615be4d3be40015ca.jpeg

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