Call Us Now
0242159130 | 0546096840
Up to 2% reward

For new all Customers

Delevery schedule

We deliver  the same day through VIP bus parcel Service in Ghana

Discount on order gift

Save ¢10.00 when you spend ¢600+! 

serial led control

serial  led control

Experiment description 5

In this lesson, we will program the Arduino UNO to achieve sending and receiving data through the serial port. The Unoboard receive datasent froma PC,thencontrolsanLEDaccordingtothereceiveddata,andatlastreturnsthe state of the LED to Serial Monitor in Arduino IDE. 

Hardware Required

- 1 * Arduino UNO

- 1 * USB Cable

- 1 * LED

- 1 * 220Ω Resistor

- 1 * Breadboard

- Several jumper wires 

Principle of operation

1 . Serial port.

Used for communication between the Arduino board and a computer or other devices.All Arduino boards have at least one serial port(also known as a UART or USART). It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB. Thus, if you use these functions, you cannot also use pins 0 and 1 for digital input or output. You can use the Arduino environment's built-in serial monitor to communicate with an Arduino board.Click the serial monitor button in the tool bar and select the same baud rate used in the call to begin(). To use these pins to communicate with your personal computer, you will need an additional USB-to-serial adaptor, as they are not connected to the UNO's USB-to-serial adaptor. To use them to communicate with an external TTL serial device, connect the TX pin to your device's RX pin, the RX to your device's TX pin,and the ground of your UNO to your device's ground.(Don't connect these pins directly to an RS232 serial port;they operate at+/-12V and can damage your Arduino board.)

2 . Key function

begin() Sets the data rate in bits per second (baud) for serial data transmission. For communicating with the computer, use one of these rates: 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. You can, however, specify other rates - for example, to communicate over pins 0 and 1 with a component that requires a particular baud rate.

Syntax:

Serial.begin(speed)

Parameters : speed: in bits per second (baud) - long

Returns : nothing 

print()

Prints data to the seria lport as human-readable ASCII text.This command can take many forms.Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is. 

For example: Serial.print(78) gives “78”

Serial.print(1.23456) gives “1.23”

Serial.print('N') gives “N” Serial.print(“Hello world.”) gives “Hello world.”

An optional second parameter specifies the base (format) to use; permitted values are BIN (binary, or base 2), OCT (octal, or base 8), DEC (decimal, or base 10), HEX (hexadecimal, or base 16). For floating point numbers, this parameter specifies the number of decimal places to use. For example:

Serial.print(78, BIN) gives “1001110”

Serial.print(78, OCT) gives “116”

Serial.print(78, DEC) gives “78”

Serial.print(78, HEX) gives “4E”

Serial.println(1.23456, 0) gives “1”

Serial.println(1.23456, 2) gives “1.23”

Serial.println(1.23456, 4) gives “1.2346”

You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example:

Serial.print(F(“Hello World”))

To send a single byte, use Serial.write().

Syntax: Serial.print(val)

Serial.print(val, format)

Parameters :

val: the value to print - any data type format: specifies the number base (for integral data types) or number of decimal places (for floating point types)

Returns  byte print() will return the number of bytes written, though reading that number is optional

println() Prints data to the serial port as human-readable ASCII text followed by a carriagereturncharacter(ASCII13,or'∖r')andanewlinecharacter(ASCII10, or '∖n'). This command takes the same forms as Serial.print().

Syntax : Serial.println(val)

Serial.println(val, format)

Parameters : val: the value to print - any data type format: specifies the number base (for integral data types) or number of decimal places (for floating point types)

Returns : byte

println() will return the number of bytes written, though reading that number is optional

read() Reads incoming serial data. read() inherits from the Stream utility class.

Syntax:

Serial.read()

Parameters:

None Returns :

the first byte of incoming serial data available(or-1 if no data is available)-int

Experiment code

//File name:   Experiment 5

//Description: If you send a character ‘1’ or ‘0’ on the  serial monitor, the status of LED will be lit or gone out.

//Website: www.okuelectronics.com

//E-mail: support@okuelectronics.com

//Author: Okudzeto osei prince

//Date: 2019/06/08


int ledpin=7;           //definition digital 7 pins as pin to control the LED


void setup()

{

  Serial.begin(9600);   // opens serial port, sets data rate to 9600 bps

  pinMode(ledpin,OUTPUT);//Set digital 11 port mode, the OUTPUT for the output

}

void loop()

{

    char receiveVal;                  // Defined receive data

   

    if(Serial.available() > 0)       //Receive serial data

    {        

        receiveVal = Serial.read();  //Save the serial data received 

        

       if(receiveVal == '1')          //Receive data is 1, lit LED lights    

       { 

           digitalWrite(ledpin,HIGH); //print out the value of the LED       

           Serial.println("LED:ON"); //send data to the serial monitor

       }

       if(receiveVal == '0')          //Receive data is 0, off LED lights

       { 

           digitalWrite(ledpin,LOW);  //print out the value of the LED                

           Serial.println("LED:OFF");//send data to the serial monitor

      } 

    }

  delay(50);                          //delay 100ms

}


Compile the program and upload to Arduino UNO board Open IDE, click to open Serial Monitor, and then select the appropriate baud rate according to the program. Now, enter '1' or '0' in the textbox on the monitor, and the LED will be turned on/off.



Add Comment