Control Your LED In The Serial Monitor Using Arduino Uno

Requirements
  1. Arduino Board
  2. Led
  3. Arduino IDE
Part 1
 
To build this circuit connect the led positive side(+) to the digital pin -13 and negative side(-) to the Gnd.
 
Ddigital pin -13
 
Figure 1: Digital pin -13
 
Part 2
 
Connect the Arduino board to your computer through USB cable it gives the external power supply to your board
 
Part 3
 
Write the following source code to control your led ON and OFF.
 
Program
  1. int led = 13;  
  2. void setup()  
  3. {  
  4.     Serial.begin(9600); //Baud Rate  
  5.     pinMode(led, OUTPUT);  
  6. }  
  7. void loop()  
  8. {  
  9.     char data = Serial.read();  
  10.     switch (data) //Selection Control Statement  
  11.     {  
  12.         case 'ON':  
  13.             digitalWrite(led, HIGH); // Sets the led ON  
  14.             break;  
  15.         case 'OFF':  
  16.             digitalWrite(led, LOW); //Sets the led OFF  
  17.             break;  
  18.     }  
  19. }  
Part 4
 
After your source code complete compile and upload your source code to the Arduino board after the uploading process. In the Arduino IDE right side corner we have the serial monitor window. Just right click and gives the command ON and press send. The LED will glow and gives command OFF; after the command the led will turn off,
 
Arduino IDE
 
Figure 2: Arduino IDE
 
Explanation
 
Set the led in the digitalpin-13 void setup() one type of function is used to initialize the variables, pinMode, etc the setup() function will run only once, after each powerup or reset of the Arduino board and void loop() it is fully based up on our conditions, how it works and what process it wants to do is called loop() function and here we are using the switch statement. It is the selection control statement based upon the condition it will work.
 
digitaWrite(led,HIGH) supplies 5 volts to the digital pin13 and digitalWrite(led, LOW) take the 13 pin back to the 0 volts, based upon these statements the led will generate the output.


Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles