Beginners Tutorials with sample codes - Part 1 CloudX

  • img
Project Details

1. LED Blink

Brief Description

For our first project, we will blink an LED. In other words, we will be turning the Light Emitting Diode (LED) on and off successively for a desired period of time. 

 

LED blinking usually is the simplest and most basic project with a microcontroller to see the effect of a physical output. It aids in readily understanding the basic working concept of the controllers in practical environment. LED Blink

Moreover, it is generally used as a kind of stepping stone example for the beginners in their microcontroller experience development. 


LED Pin-Out

Identifying the right polarity of the LED is simply straight forward:

  • the cathode (-) lead is typically the shorter one of the two;
  • or the one located at the flat side of the LED.

 

Wiring

 

Code

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

setup(){
//setup here

pinMode(LED, OUTPUT); //configures digital I/O pin 1 (LED)
//as an OUTPUT pin

loop(){
//Program here

digitalWrite(LED, ON); //turns on LED
delayMs(1000); //keeps LED on for 1000
//milliseconds (1 sec)

digitalWrite(LED, OFF); //turns off LED
delayMs(1000); // keeps LED off for 1 sec
}
}
 

 

Simulation

 

 

 

2. LED ON/OFF Control with a Push-button

Brief Description

This project turns on and off an LED with respect to the current LED digital logic state, whenever the button-switch is pressed. In other words, the switch simply toggles the LED ON/OFF.

A push-buttonis a mechanical component that makes/breaks contacts with two or more points in a circuit, whenever it is pressed. 

 

In the first project, we used one of the digital I/O (Input/Output) pins of the CloudX as an output to blink our LED. We could as well use these same pins to accept Input from users. But, in order to be able to do that, they (the pins) have to be accordingly configured.

The pinMode function is utilized to configure the pins either as:

  • an INPUT, like when detecting whether a push button is been pressed by a user, or taking a reading from a sensor;
  • or as an OUTPUT, like in our previous example of LED blink.

Note

pinxMode = OUTPUTand pinxMode = INPUT can also be used to configure any digital I/O pin as an OUTPUT or INPUT pin just like the pinMode function. 

A phenomenon known as Switch Bounce is a hardware issue that needs to be taken care of.


Luckily enough, we use the code to resolve that!

 

Tactile-Switch Pin-Out

 

Wiring

Code

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

#define BUTTON 2 //2 defined as BUTTON corresponding to
//the pin where it is connecting to

setup(){
//setup here

pin1Mode = OUTPUT; //configures digital I/O pin1 (LED)
//as an OUTPUT pin

pinMode(BUTTON, INPUT); //configures digital I/O
//pin2(BUTTON) as an INPUT pin

LED = OFF; //clears/turns off digital pin1

loop(){
//Program here

//------------------------------------------------------
//Execute this section of code, if button is pressed and
//pin1 is OFF.
//------------------------------------------------------
if(readPin(BUTTON) is LOW and LED is OFF)
{
delayMs(250); //switch debounce delay
LED = ON; //turns on digital pin 1
}

//------------------------------------------------------
//Execute this section of code, if button is pressed and
//pin1 is ON.
//------------------------------------------------------
if(readPin(BUTTON) is LOW and LED is ON )
{
delayMs(250); //switch debounce delay
LED = OFF; //turns off digital pin 1
}

}
}
 

 

Simulation

 

 

3. Traffic Light 

Brief Description

In this project, we’ll create a simple Traffic Light. 

Principle of Operation

When the Red light is on, the Green light is off and vice versa; whereas the Yellow light comes on, when the Red and Green Lights are about to change status. 

Wiring

 

Code

#define RED    1    //digital pin 1 to the Red LED  
#define YELLOW 2 //digital pin 2 to the Yellow LED
#define GREEN 3 //digital pin 3 to the Green LED

setup(){

//The below configure pin 1 -> pin 3 as OUTPUT pins
pin1Mode = OUTPUT;
pin2Mode = OUTPUT;
pin3Mode = OUTPUT;

digitalWrite(RED, OFF); //turns off RED LED
digitalWrite(YELLOW, OFF); //turns off YELLOW LED
digitalWrite(GREEN, OFF); //turns off GREEN LED

loop(){
//Program here

digitalWrite(RED, ON); //turns on RED LED

delayMs(1500); //waits for 1.5 seconds

digitalWrite(RED, OFF); //turns off RED LED

digitalWrite(YELLOW, ON); //turns on YELLOW LED

delayMs(500); //waits for half a second

digitalWrite(YELLOW, OFF); //turns off YELLOW LED

digitalWrite(GREEN, ON); //turns on GREEN LED
delayMs(1500); //waits for 1.5 seconds

digitalWrite(GREEN, OFF); //turns off GREEN LED

digitalWrite(YELLOW, ON); //turn on YELLOW LED

delayMs(500); //waits for half a second

digitalWrite(YELLOW, OFF); //turns off YELLOW LED

}
}
 

 

 

 

Simulation

 

 

 

4. 7-Segment 0 - 9 Counter 

Brief Description

In this project, we’ll use a 7-Segment LED Display Unit to count from 0 to 9. A 7-segment LED display consists of eight in-built LEDs, and it is perfect for displaying numbers. 

 

Usually, the anodes or cathodes of the member LEDs are internally connected together for ease of usage; and hence, the segment display is referred to as either a Common-Anode or a Common-Cathode type. 

For our project, we’ll use the Common-Cathode type. 

The common cathode module has an anode pin for each LED segment. 

 

7-Segment Display Unit Pin-Out

The 8 LEDs are labeled from A to G and DP (for the decimal point).

 

Wiring

Code

#include <CloudX\Segment.h>  


char NumberOfDigit = 1; //sets number of 7 segment displays
//to be used

//-----------------------------------------------------------
//Connect these CloudX pins to the Data Pins A,B,C,D,E,F and
//and G pins of the Display. (DP pin is not in use.)
//-----------------------------------------------------------
char segmentDataPins[]= {1, 2, 3, 4, 5, 6, 7, NULL};

//-----------------------------------------------------------
//Connect these CloudX pins to the Common Anode or Cathode of
//each 7-segment display.
//-----------------------------------------------------------
char segmentScanPins[]= 0; //to save pins we connect directly
//to ground(cathode)

setup(){
//setup here

//---------------------------------------------------
//Initialize the 7 segment Display with these data.
//---------------------------------------------------
Segment_setting(CCathode,NumberOfDigit,segmentScanPins,segmentDataPins);

loop(){
//Program here
Segment_write(0, 1000); //writes 0 on 7-segment display
//for 1 sec

Segment_write(1, 1000); //writes 1 on 7-segment display
//for 1 sec

Segment_write(2, 1000); //writes 2 on 7-segment display
//for 1 sec

Segment_write(3, 1000); //writes 3 on 7-segment display
//for 1 sec

Segment_write(4, 1000); //writes 4 on 7-segment display
//for 1 sec

Segment_write(5, 1000); //writes 5 on 7-segment display
//for 1 sec

Segment_write(6, 1000); //writes 6 on 7-segment display
//for 1 sec

Segment_write(7, 1000); //writes 7 on 7-segment display
//for 1 sec

Segment_write(8, 1000); //writes 8 on 7-segment display
//for 1 sec

Segment_write(9, 1000); //writes 9 on 7-segment display
//for 1 sec
}
}

 

Simulation

 

You can ask questions in the comment space below.

Share this with friends