Reading Switches With PIC Microcontroller – FlowcodeBitahwa Bindu
Watch The Video Tutorial
Switches are digital inputs and are widely used in electronic projects as most systems need to respond to user commands or sensors.
The simplest form of a user command is by switching on and off a switch, this is a good example of a digital input which is basically anything that makes or breaks a single connection, or something that can have only two states: On or Off, High or Low, 0V or +5V etc.
Connecting a switch is very useful because a switch can represent a wide range of digital devices in real world like limit sensors, level switches, proximity switches, keypads (a combination of switches) etc.
With flowcode it is very easy to connect a switch to any PORT of microcontroller all you need to do is to click on a switch component and then drag and drop an Input Icon on the flowchart.
Let us create a circuit as shown on the figure above:
- Two coloured LEDs (Red and Green) are connected to PORTD.
- Two toggle switches are connected to PORTC.
- If a switch is closed, it corresponding LED will switch ON.
- 10K resistors are used as pull up resistors and 220 as series resistor for the LEDs.
- Note that the MCLR pin is also connected to the supply voltage via a 10K resistor and external oscillator created by X1 is also used instead of internal oscillator.
—>>Start a new project select two LEDs (Red and Green) connect them to PORTD.
—>>Select a Switch component from the components tool bar.
—>> select a switch then click on the “….” next to connections properties to open its connection properties.
Connect the first switch to PORTC Bit 0 and the second one to PORTC Bit 1.
—>>Click again on the “….” next to the Ext Properties to edit the switch properties:
Type: You can select either a Toggle switch, Push button, Slide, Rocker, Rotary or you can customise your own.
Switch Operation: Controls how the switch on the panel behaves when it is pressed.
Latching – Switch stays in the state it is currently in until manually changed by the user.
Momentary – Switch always goes back to the default state after the switch has been released by the user.
Polarity: configures if the switch creates a logical 1 when pressed (Active High) or a logical 0 when pressed (Active Low).
Debounce: The switch debounce time period in milliseconds (ms).
Debounce is used with the WaitUntilLow and WaitUntilHigh macros to ensure a change has occurred rather than switch bounce or voltage irregularities.
—>>On the flowchart, insert a calculation icon and declare four variables: RED with an initial value of 1 (to switch on Red LED), GREEN with a value of 2 (for the Green LED), BOTH to switch ON both LEDs and Switch_Pressed to check which switch that was pressed.
—>>Insert an INPUT Icon to input the state of the PORT where your switches are connected.
—>>Insert Decision Icons as learned in the Flowcode Decision article to find out which switch that was pressed.
Download Reading Switches Flowcode Project