Switches and push buttons: Difference between revisions
(3 intermediate revisions by the same user not shown) | |||
Line 22: | Line 22: | ||
[[File:4-band-resistor-chart.svg|thumb|Resistor color codes.]] | [[File:4-band-resistor-chart.svg|thumb|Resistor color codes.]] | ||
The external resistor should be somewhere between 4k and 20k Ohms. | The external resistor should be somewhere between 4k and 20k Ohms. | ||
* | *1kΩ: Brown, Black, Red | ||
* | *2kΩ: Red, Black, Red | ||
* | *10kΩ: Brown, Black, Orange, (Gold). | ||
*20kΩ: Red, Black, Orange | |||
*50kΩ: Green, Black, Orange | |||
*100kΩ: Brown, Black, Yellow | |||
== Multiple buttons on One Analog Pin == | == Multiple buttons on One Analog Pin == | ||
Line 32: | Line 35: | ||
Using different resistors on push buttons gives possibility to use many buttons with one analog input only. | Using different resistors on push buttons gives possibility to use many buttons with one analog input only. | ||
The proposed resistances are | The proposed resistances are (https://www.instructables.com/How-to-Multiple-Buttons-on-1-Analog-Pin-Arduino-Tu/) | ||
* <math>R_1 = 1 </math>k | * <math>R_1 = 1 </math>k | ||
* <math>R_2 = 2 </math>k | * <math>R_2 = 2 </math>k | ||
* <math>R = 100 </math>k | * <math>R = 100 </math>k | ||
<syntaxhighlight lang="C"> | |||
void setup() { | |||
Serial.begin(9600); | |||
} | |||
void loop() { | |||
Serial.println( analogRead(A0) ); | |||
delay(100); | |||
} | |||
</syntaxhighlight> |
Latest revision as of 21:46, 26 July 2023
Introduction

The idea is given in the image below. If the switch is open, the pin is connected through the resistor to 5V, but when switch is closed, the pin is grounded to 0V. The resistor is called pull-up resistor, and prevents shorting to ground when the switch is closed. The other method is pull-down resistor.
See more different switches at https://learn.sparkfun.com/tutorials/button-and-switch-basics/all
Push Button

A double pole single throw push button micro switch (DPST) is a widely used button. Push button switches are often color coded to indicate their function. The opposite pins are connected, see https://docs.wokwi.com/parts/wokwi-pushbutton
Three methods to wire because Arduino has built-in internal resistors:
- External pullup or pulldown resistor: Use
pinMode( Button_pin, INPUT);
in the setup. - Internal resistor (20k-50k Ω): Use
INPUT_PULLUP
in setuppinMode( Button_pin, INPUT_PULLUP);
. The default value isHIGH
(1) and by pressing the button the state goes toLOW
. - The bad method: Use
pinMode( Button_pin, INPUT);
in the setup without any resistors. The default value will float between 0V and 5V: there is no internal or external voltage reference for the push button.

The external resistor should be somewhere between 4k and 20k Ohms.
- 1kΩ: Brown, Black, Red
- 2kΩ: Red, Black, Red
- 10kΩ: Brown, Black, Orange, (Gold).
- 20kΩ: Red, Black, Orange
- 50kΩ: Green, Black, Orange
- 100kΩ: Brown, Black, Yellow
Multiple buttons on One Analog Pin

Using different resistors on push buttons gives possibility to use many buttons with one analog input only.
The proposed resistances are (https://www.instructables.com/How-to-Multiple-Buttons-on-1-Analog-Pin-Arduino-Tu/)
- k
- k
- k
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println( analogRead(A0) );
delay(100);
}