CloudX Hello-World LED-Blink using a Switch CloudX

  • img
Project Details

 

Nota Bene

 

 

Let's learn how to blink an LED using a switch! We’ll be making use of the button switch also known as the tactile switch. Here, we will also be connecting up a very simple circuit using the breadboard while as well make use of a very few lines of code to control a single LED on and off.

So far, we have learnt how to blink a simple LED; and in this lesson, we’ll extend it further to using a switch to control the ON and OFF states of the LED. We initially saw how to configure the digital I/O (input/output) pin of the CloudX board as an output pin to simply take care of our output LED display unit. Here, another pin will now be configured as an input pin to adequately handle the switch which is, in this case, used as an input unit.

List Of Materials Needed 

(You can readily get the project full kit via this link.)

  • (i)  CloudX Microcontroller and Softcard
  • (ii)  An LED
  • (iii) Resistor:
  1. *any value from 100Ω – 1kΩ
  2. *10 kΩ
  • (iv) A Tactile (button) Switch

 

Material

 

 

 

                             Step 1:  Hints and Brief Overview 


The tactile switch we are using is a push-to-make micro switch. It has 2 pairs of pins, with the pins in each pair connected to each other.  

Button Switch
Button Internal Architecture

Hint:

  • The pins visibly wider apart from each other form a pair in the internal architecture. Thus, refer to the below illustration.
Button Pin-out

 

 

                            Step 2: Build The Circuit

 

Fritzing Wiring

 

LED Control using Switch

 

As illustrated in the schematic diagram above, the LED connects up to the microcontroller board via a 330Ω through to p1 of the digital I/O ports. 

Likewise, the additional section of the circuit, comprising the switch and another resistor, connects up to the board at the p2. Here, the resistor is used in the pull-up mode both for the switch and the input pin. Meanwhile, a pull-up resistor typically takes high (ie. raises to VCC level) the particular pin of interest pending a switch press when it (the pin) is dropped low to the power ground level. 

(Or alternatively, you could make use of an on-board LED attached to p1 of the same digital I/O ports for this blink-LED project).

On-Board LED

Power can be sourced to the board either via the USB Softcard; or via connecting an external power supply (between 7 – 12 VDC) to the VIN pin of the microcontroller board.

 

                         Step 3: Build The Source Code

The code is written in plain C Language.

buton_code

 

Let’s try to dissect the code in more detail:

Note:

The double slash // is a precursor to a comment. Comments are meant to just allow the reader readily understands what a certain part of the code is intended to; and hence not part of the actual program the CloudX executes. 

 

The set-up section of the code brings to light a two alternative way digital I/O (Input-Output) pins could be typically configured. Thus:

 

Method I

Method I

(or pin1Mode = INPUT)

 

Method II

Method II

ie. pinMode(2, INPUT)

(or pinMode(button,  OUTPUT);

ie. pinMode(2,  OUTPUT);  )

 

The extra kick in the code, standing it side by side with the previous simple LED-blink project, is the part that has to do with the switch. Here in the loop section, the program waits for an actual switch-press before setting or clearing the voltage/logic level of the output pin to which the LED is connected.

Loop Section

        

The delay line takes care of contact bounce.  The contact bounce is a common phenomenon with mechanical switches; making their contacts momentarily bounce ON and OFF, before finally settling down, whenever they are initially engaged. This constitutes a problem to the fast responsive microcontroller circuits, which makes them end up misinterpreting the false on/off pulses as valid data stream.

Switch Bounce

 

A very quick solution to that is to apply a software debounce, whereby a certain delay is implemented after a change in the logic level of an input is initially detected. This allows the contact-bounce (in the switch) enough time to settle down before a follow up action (in the code) is to be executed.

 

                             Step 4: Compile The Code

Now the source code is ready, it’s compilation time. The code is accordingly compiled to get a hex file that will be later burnt into the microcontroller.

For further details on how to compile the code into an executable (hex) file, you can refer to this link.

 

                           Step 5: Load In The Hex Code File

At this stage, the hex file is loaded (or burnt) into the microcontroller for the final project power  up. 

For further details on how to burn in the code to the microcontroller, you can refer to this link.

 

                              Step 6: Power Up The Project

The project is finally powered on, and should operate as specified in our design.

LED Control using Switch Simulated

 

 

 

Now that we have learnt how to add a switch to the simple LED project in order to control its ON and OFF states, we are now ready to extend yet again our knowledge of the LED control. This time, we will be meeting a LED display called the 7-segment display. It is just a display unit comprising 8 LEDs molded together.  Continue on to the next lesson as we further attempt to explore embedded systems design using CloudX.

 

------------------------------------------------------

Copy Code

 

#define LED pin1       //anytime the compiler sees the text LED,  
//it replaces it with pin1

#define button 2 //digital pin2 defined as button

setup(){
//setup here

pin1Mode = OUTPUT; //configures digital I/O pin 1 (LED) as an OUTPUT pin
pinMode(button, INPUT); //sets up digital I/O pin2 (button) as an INPUT pin
LED = OFF; //clears (turns off) digital pin 1

loop(){
//Program here

//execute this block of codes, if: (i)switch is pressed and (ii)pin1 is OFF
if((readPin(button) is LOW) and (LED is OFF))
{
delayMs(120); //switch-debounce delay
LED = ON; //turns on digital pin 1
}

//execute this block of codes, if: (i)switch is pressed and (ii)pin1 is ON
if((readPin(button) is LOW) and (LED is ON ))
{
delayMs(120); //switch-debounce delay
LED = OFF; //turns off digital pin 1
}

}
}
 

---------------------------------------------------                      

Share this with friends