Neccesary knowledge:
User Interface Module,
Registers,
Digital Inputs/Outputs,
Pins,
Light-emitting Diode
A switch is an electromagnetic device which can connect or break an electrical circuit. There are many different types of switches; the most common mechanical switch is mechanically connectible electrical connectors. If connectors are connected, electrical circuit is closed and electricity can flow through the switch. If connectors are disconnected, electricity cannot flow through the switch.
Switches are commonly used to electrify electrical circuits, but can also be used as sensors. Switch as a sensor is also the subject of this exercise and therefore examining specific high-voltage and high-amperage switches has been excluded. Switches can be differentiated by the number of contacts and by their connection method. Different types include: two contact switches and double switches, where contact pairs are connected, but also push button, sliding, rocker and toggle switches as well as switches which disconnect instead of connect electrical circuit.
Different schematic symbols are used for identifying different types of switches. Below are examples of typical electrical switches used in electrical drawings with their symbols:
In order to use a switch as a sensor connected to a microcontroller, one contact of the switch is connected to the microcontrollers pin and is set as input in the program. If contact is connected to the ground or input potential, the bus’s bit rate of microcontroller’s pin is changed. It would sound logical to use toggle switch, which allows connecting one contact with desired contact (in this case ground or input). For our purpose it is not as simple, since on the moment of shifting of the connections the contacts are not connected. The moment itself is very short (milliseconds), but during this moment microcontrollers input pin is connected to nowhere and therefore has indefinite value. Due to electromagnetic interference (which exists everywhere) input pin that is connected to nowhere can have a value of 0 or 1 at random moments in time.
The interferences complicate the usage of switches. Most common method for avoiding the undetermined results is to connect microcontroller’s input to the ground or input potential through a resistor. A resistor for this function is called pull-down or pull-up resistor. Usually the resistance of pull-down or pull-up resistor varies from 1 kΩ to 1 MΩ. With open switch, the input is charged with voltage of the resistor, by closing the switch, the input receives the voltage of the switch since the resistivity of the switch is a lot smaller (close to zero) compared to the one of the resistor. Basically it can be referred to as a volt-box.
A simple two contact switch can be used as a sensor with pull-up or pull-down resistor, switch connects input with one and resistor to the other potential. Usually microcontrollers have built-in pull-up or pull-down resistor option; therefore, there is no need to add a resistor to the circuit. For example, AVR microcontrollers have 20 kΩ – 50 kΩ pull-up resistors on their IO pins. It must be mentioned that, mechanical switches have one more problem – switch bounce. This causes several very short missconnections at the moment of connection. It must be emphasized, that the examples used in this chapter are not affected by the switch bounce and the issue will be covered in more detail in the next chapter.
There are three push button-switches on the Digital i/o module. Those switches connect the pins of microcontroller to the earth, however not directly through a resistor, this is to avoid short circuiting when pressing the button while setting the pins as output. The switches have also pull-up resistors, but those have much greater resistance than protective resistors, therefore while pressing the button, there will still be approximately 0 V voltages on the corresponding pin.
The switches are on PC0, PC1 and PC2 pins. In order to read the status of the switches, the corresponding pins of the microcontroller must be set as inputs. There is no need to put AVR internal pull-up resistors into operation, because the pins already have external resistors. When the button is pressed down, the corresponding bus of the pin has value 0, when the button is released, the value is 1. LED indicators can be used to see if the microcontroller understood the buttons action.
Sample code for using buttons is based on the HomeLab pins library, which was introduced in the example of LED.
// // Program for testing the buttons of the Digital i/o module // #include <homelab/pin.h> // // Determining the pins of LED-d and buttons. // pin leds[3] = { PIN(C, 5), PIN(C, 4), PIN(C, 3) }; pin buttons[3] = { PIN(C, 2), PIN(C, 1), PIN(C, 0) }; // // Main program // int main(void) { unsigned char i; // Setting the LED pins as outputs and buttons pins as inputs. for (i = 0; i < 3; i++) { pin_setup_output(leds[i]); pin_setup_input(buttons[i]); } // Endless cycle while (true) { // Each button has a corresponding LED, // which lights when button is pressed. for (i = 0; i < 3; i++) { pin_set_to(leds[i], pin_get_value(buttons[i])); } } }
In the given example LEDs and buttons are defined as array – this allows them to be use in for loop. When starting the program, LED pins are set as output and buttons as input. In the inter programs endless cycle a constant acquiring of the state of buttons is performed which in turn determines the state of corresponding LEDs. First button is lighting green LED, second yellow and third red.