Push Button Interfacing PDF

Document Details

ChivalrousAstatine9861

Uploaded by ChivalrousAstatine9861

Technological Institute of the Philippines

Tags

arduino programming electronics microcontroller digital circuits

Summary

This document provides information about push button interfacing, including switch types, functions, and examples in Arduino programming. It covers concepts like push-to-make, push-to-break switches, pull-up and pull-down resistors, and functions like digitalRead().

Full Transcript

10/27/24, 7:46 PM OneNote Push Button Interfacing Sunday, 27 October 2024 4:42 pm Switch Note: De...

10/27/24, 7:46 PM OneNote Push Button Interfacing Sunday, 27 October 2024 4:42 pm Switch Note: Description: It is used to accept inputs from the external world and process it to the All switches work on the same basic principle: it either "closes the circuit" and turns program inside the microcontroller and execute a routine for such something on, or "opens the circuit" and turns something off. conditions. "Closes the circuit" Electricity can flow through; switch is close. Push Button Switch "Opens the circuit" Description: Electricity cannot flow through; switch is open. A momentary switch that changes state temporarily. Examples of Switches: Note: An automatic mechanism forces the switch to return to its initial position Push-button immediately after the switch was released. Toggle Rotary Push To Make Switch Magnetic Switch Description: It allows the electricity to flow between its two contacts when pressed. It is also known as Normally Open (NO) Switch. Examples: Keypad, doorbell, CPU power switch, calculator buttons Push To Break Switch Description: Allows the flow of electricity between two contacts when the button is not pressed. Also known as Normally Closed (NC) Switch. Examples: Refrigerator light switch, OFF or stop switch in industrial circuits digitalRead() Function Description: Reads the value from a specified digital pin and return its status which is either HIGH or LOW Syntax: Note: digitalRead( pin ) Many push buttons can function as both 'push to make' and 'push to break' ○ Pin: the Arduino pin number you want to read switches. The connection of the switch determines whether it will act as NO Example: or NC. Sets pin 13 to the same value as pin 7, declared as an input. https://onedrive.live.com/redir?resid=A380C2F5F08CF942%21686100&authkey=%21AO0Sbn2DsI33kp4&page=View&wd=target%28Microprocessor.one%7Cc7d4f054-fd3c-4d40-a0ca-ec3166f42179%… 1/3 10/27/24, 7:46 PM OneNote Pull-up Resistors int ledPin = 10; // LED connected to digital pin 10 Description: int inPin = 7; // pushbutton connected to digital pin 7 Resistors that forces the wire signal to pull to a logic ONE (1) or ON in int val = 0; // variable to store the read value the absence of an input signal. void setup() { The output is HIGH if the switch is unpressed (OPEN) and becomes LOW when pinMode(ledPin, OUTPUT); // sets the digital pin 10 as output the switch is pressed (CLOSE). pinMode(inPin, INPUT); // sets the digital pin 7 as input } void loop() { val = digitalRead(inPin); // read the input pin digitalWrite(ledPin, val); // sets the LED to the button's value } If(condition) Statement Description: It checks for a condition and executes the proceeding statement or set of statements if the condition is 'true'. Syntax: If(condition) { //statement(s) } Condition: a boolean expression which can be true or false Conditions you can use: x == y (x is equal to y) Pull-down Resistors x != y (x is not equal to y) Description: x < y (x is less than y) Resistors that forces the wire signal to pull down to logic ZERO (0) or x > y (x is greater than y) OFF in the absence of an input signal. x = y (x is greater than or equal to y) The output is LOW if the switch is unpressed (OPEN) and becomes HIGH when the switch is pressed (CLOSE). Example: if (btnStatus== HIGH) { digitalWrite(LEDpin1, HIGH); digitalWrite(LEDpin2, HIGH); } https://onedrive.live.com/redir?resid=A380C2F5F08CF942%21686100&authkey=%21AO0Sbn2DsI33kp4&page=View&wd=target%28Microprocessor.one%7Cc7d4f054-fd3c-4d40-a0ca-ec3166f42179%… 2/3 10/27/24, 7:46 PM OneNote https://onedrive.live.com/redir?resid=A380C2F5F08CF942%21686100&authkey=%21AO0Sbn2DsI33kp4&page=View&wd=target%28Microprocessor.one%7Cc7d4f054-fd3c-4d40-a0ca-ec3166f42179%… 3/3 10/27/24, 7:56 PM OneNote Keypad Interfacing Sunday, 27 October 2024 5:54 pm Numeric Keypad Keypad Library Description: Description: Used in many electronic devices. The library is non-blocking Most commonly used in cellphone; used both as a numeric keypad and as a way to type text instead of ○ Means that you can press and hold the key and your Arduino will numbers. continue processing the rest of the code. Has 12 or 16 buttons that can be connected to the microcontroller by only 8 lines. This is solved by using multiplexing. Note: When writing the code, every delay you use will take processing time away from the keypad. Something as short as delay(250) can make the keypad seem very unresponsive. The same thing will happen if you sprinkle a bunch of delay(10)'s through the code. The function getKey() returns a key value as soon as you press the key but it does not repeat automatically. You can also track the key released event if you are using the eventListener feature of the library. Example: #include const byte ROWS = 4; //four rows const byte COLS = 4; //four columns char keys[ROWS][COLS] = { {'1', '2', '3', 'A'}, {'4', '5', '6' ,'B'}, {'7', '8', '9', 'C'}, {'*', '0' ,'#' ,'D'} }; byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); void setup() { All switches are open initially so there is no connection between the rows and columns. When any of the Serial.begin(9600); switches are pressed, the corresponding row and column are connected (short circuited). This will drive that } column pin low. void loop() Through this logic, the button press can be detected. { char key = keypad.getKey(); if (key != NO_KEY) { Serial.println(key); } } https://onedrive.live.com/redir?resid=A380C2F5F08CF942%21686100&authkey=%21AO0Sbn2DsI33kp4&page=View&wd=target%28Microprocessor.one%7Cc7d4f054-fd3c-4d40-a0ca-ec3166f42179%… 1/2 10/27/24, 7:56 PM OneNote https://onedrive.live.com/redir?resid=A380C2F5F08CF942%21686100&authkey=%21AO0Sbn2DsI33kp4&page=View&wd=target%28Microprocessor.one%7Cc7d4f054-fd3c-4d40-a0ca-ec3166f42179%… 2/2 10/27/24, 7:53 PM OneNote LCD Interfacing Sunday, 27 October 2024 5:11 pm Liquid Crystal Display(LCD) LiquidCrystal() Function Description: Description: A display device used in digital circuits for displaying messages. Creates a variable of type LiquidCrystal. Usually presents as an LCD Module which integrates the driver and the The display can be controlled using 4 or 8 data lines. If former, leave the lines for the pin controller into a package. numbers d0 to d3 unconnected. The RW pin can be tied to ground instead of connected to a pin on the Arduino. Omit these from the function's parameter list. Syntax: LiquidCrystal lcd(rs, en, d4, d5, d6, d7) LiquidCrystal lcd(rs, rw, en, d4, d5, d6, d7) LiquidCrystal lcd(rs, en, d0, d1, d2, d3, d4, d5, d6, d7) LiquidCrystal lcd(rs, rw, en, d0, d1, d2, d3, d4, d5, d6, d7) lcd - the variable rs - Arduino pin connected to the RS pin on the LCD rw - Arduino pin connected to the RW pin on the LCD (optional) Enable or en - Arduino pin connected to the enable pin on the LCD d0-d7 - Arduino pins that are connected to the corresponding data pins on the LCD. Note: d0 - d3 are optional. If omitted or unincluded, the LCD will be controlled using only the four data lines (d4 to d7) Example: #include A 16x2 LCD means that it has 2 rows and 16 maximum characters for each LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); row. void setup() LCD Display does not produce light itself, so it needs illumination. { lcd.begin(16,1); lcd.print("hello, world!"); Backlight } Description: It is a special light source of illumination used in LCD void loop() It illuminates the LCD from the side or the back of the display screen. { Note: } A standard 16x2 LCD has 16 pins, labeled 1 on the leftmost pin and labeled 16 on the rightmost pin. begin() Function Description: Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display. It needs to be called before any other LCD library commands. Syntax: myLCD.begin(cols, rows) myLCD - variable name https://onedrive.live.com/redir?resid=A380C2F5F08CF942%21686100&authkey=%21AO0Sbn2DsI33kp4&page=View&wd=target%28Microprocessor.one%7Cc7d4f054-fd3c-4d40-a0ca-ec3166f42179%… 1/4 10/27/24, 7:53 PM OneNote cols - the number of columns that the display has rows - the number of rows that the display has Example: //Specify a LCD display of 16x2 LCD myLCD.begin (16,2); RS, RW, and E pins are the control pins. clear() Function DB0 to DB7 pins are the command/data bus Description: Vdd and Vss are power supply pins Clears the LCD screen and positions the cursor in the upper-left corner(0,0). Vo pin is the contrast control pin Syntax: A and K are backlight power pins myLCD.clear(); Note: myLCD - variable name The contrast of 16x2 LCD is inversely proportional to the voltage applied to the Vo pin. Maximum contrast is obtained by wiring the Vo pin to ground. setCursor() Function Description: Arduino LCD Library Positions the LCD cursor Description: Set the location at which subsequent text written to the LCD will be displayed. It allows the Arduino board to control Liquid Crystal Displays (LCDs) based on the Hitachi HD44780 (or a compatible) chipset which is found Syntax: on most text-based LCDs. lcd.setCursor(col,row) It works with either 4- or 8-bit mode (using 4 or 8 lines in addition to the RS, EN, and optionally, the RW control lines. lcd - variable name col - the column at which to position the cursor. (with 0 being the first column) To use the Liquid Crystal library functions in a sketch, add the library using row - the row at which to position the cursor. (with 0 being the first row) #include directive at the global declaration. Example: 1. Include the Liquid Crystal.h header file: //Set the cursor to colum 4, line 1 #include lcd.setCursor(4,1); 2. Create a LiquidCrystal object using the LiquidCrystal() function. print() Function LiquidCrystal var_name(parameter_list); Description: Prints text to the LCD. Syntax: lcd.print(data) lcd.print(data, BASE) lcd - variable name data - the data to print (char, byte, int, long, or string) BASE(optional) - the base in which to print numbersL BIN for binary(base 2), DEC for decimal(base 10), OCT for octal(base 8), HEX for hexadecimal(base 16). Example: #include LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); void setup() { lcd.begin(16,2); lcd.print("hello, world!"); } https://onedrive.live.com/redir?resid=A380C2F5F08CF942%21686100&authkey=%21AO0Sbn2DsI33kp4&page=View&wd=target%28Microprocessor.one%7Cc7d4f054-fd3c-4d40-a0ca-ec3166f42179%… 2/4 10/27/24, 7:53 PM OneNote void loop() {} for() Loop Statement Description: Used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop. It is useful for any repetitive operation, and is often used in combination with arrays to operate on collections of data/pins. Syntax: For(initialization; condition; increment) { //statement(s); } Initialization - happens first and exactly once Condition - the statement to be tested each time the loop goes back to the start. If it's true, the statement block, and the increment is executed. If the condition becomes false, the loop will end. Increment - executed each time through the loop when condition is true. Example: //Set pins 4 to 8 as outputs void setup() { for( int pin=4;pin

Use Quizgecko on...
Browser
Browser