Exp 7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Exp.

7: SERIAL COMMUNICATION

Aim:
1. To establish serial communication between Arduino Uno and PC
2. To receive a number through serial communication and display it in 7 segment display.

Components Requirement:

Hardware components : i) Desktop/ Laptop (Host)


(ii) Arduino Uno Development Board (Target)
(iii) Type B USB Cable
Software Tools : Arduino IDE,
Tinkercad online Virtual Lab (For simulatuion)

Theory:

Arduino is an open-source prototyping platform in electronics based on easy-to-use hardware


and software. The communication between arduino and computer is established at a specific
baud rate. The baud rate specifies how fast the data is sent over the serial line or in simple
terms, the speed of serial communication. Some common rates for UART are 9600 baud,
11520 baud etc. To start serial communication the baud rate set for arduino and computer
must be the same, if baud rate for both is set at 9600 baud, then to transmit 1 bit of data it will
take 1/9600 sec = 0.014 msec.The main purpose of this serial communication is to transfer
the sketch from computer to Arduino, send information to computer etc.The most common
type of serial communication protocol is UART i.e. Universal Asynchronous Receiver
Transmitter. The UART connection requires a 9 pin DE – 9 Connector. But, most modern
computers and laptops do not include these COM ports. Hence, if we want to connect our
microcontrollers like 8051, AVR or ARM, we need to use an external serial to USB
converter.But Arduino UNO has an on board serial to USB converter and hence we can
directly connect the Arduino to the computer. Using this USB connection and Arduino IDE,
we can send data to Arduino or receive data from Arduino. This data can be monitored with
the help of Serial Monitor in the Arduino IDE.

Code Structure
void setup() {
// put your setup code here, to run once:

void loop() {
// put your main code here, to run repeatedly:

Bare minimum code

● setup : It is called only when the Arduino is powered on or reset. It is used to initialize
variables and pin modes
● loop : The loop functions runs continuously till the device is powered off. The main logic of
the code goes here. Similar to while (1) for micro-controller programming.

PinMode

● A pin on arduino can be set as input or output by using pinMode function.


● pinMode(13, OUTPUT); // sets pin 13 as output pin
● pinMode(13, INPUT); // sets pin 13 as input pin

Pre Lab:
1. What is serial communication?
2. What is baud rate?

Lab Procedure:
1. Connect the Arduino Uno Development Board to the Desktop/Laptop through Type B
USB cable.
2. Write the code and compile it in Arduino IDE.
3. Select USB port in Arduino IDE.
4. Select Arduino Uno Development Board in Arduino IDE.
5. Download the code into Arduino Uno Development Board
6. Run the code in Arduino Uno Development Board.
7. Observe and verify the output.

Post Lab:
1. How do you establish serial communication between Arduin and PC?
2. In the code, explain the statement
COUNT = Serial.read()-48;

Output:

Seven segment display shows the input number


Paste your Screenshots here.
CODE:

#define segA 6//connecting segment A to PIN6


#define segB 7// connecting segment B to PIN7
#define segC 8// connecting segment C to PIN8
#define segD 9// connecting segment D to PIN9
#define segE 10// connecting segment E to PIN101.
#define segF 11// connecting segment F to PIN11
#define segG 12// connecting segment G to PIN12
int COUNT=7;//count integer for 0-9 increment
void setup()

{
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);

//Initialize serial and wait for port to open:


Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Welcome all. I am Arduino");
}
void loop()
{
switch (COUNT)
{
case 0://when count value is zero show”0” on disp
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, LOW);
break;

case 1:// when count value is 1 show”1” on disp


digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;

case 2:// when count value is 2 show”2” on disp

digitalWrite(segA, HIGH);

digitalWrite(segB, HIGH);

digitalWrite(segC, LOW);

digitalWrite(segD, HIGH);

digitalWrite(segE, HIGH);

digitalWrite(segF, LOW);

digitalWrite(segG, HIGH);

break;

case 3:// when count value is 3 show”3” on disp

digitalWrite(segA, HIGH);

digitalWrite(segB, HIGH);

digitalWrite(segC, HIGH);

digitalWrite(segD, HIGH);

digitalWrite(segE, LOW);

digitalWrite(segF, LOW);

digitalWrite(segG, HIGH);
break;

case 4:// when count value is 4 show”4” on disp

digitalWrite(segA, LOW);

digitalWrite(segB, HIGH);

digitalWrite(segC, HIGH);

digitalWrite(segD, LOW);

digitalWrite(segE, LOW);

digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;

case 5:// when count value is 5 show”5” on disp

digitalWrite(segA, HIGH);

digitalWrite(segB, LOW);

digitalWrite(segC, HIGH);

digitalWrite(segD, HIGH);

digitalWrite(segE, LOW);

digitalWrite(segF, HIGH);

digitalWrite(segG, HIGH);

break;

case 6:// when count value is 6 show”6” on disp

digitalWrite(segA, HIGH);

digitalWrite(segB, LOW);

digitalWrite(segC, HIGH);

digitalWrite(segD, HIGH);

digitalWrite(segE, HIGH);

digitalWrite(segF, HIGH);

digitalWrite(segG, HIGH);

break;

case 7:// when count value is 7 show”7” on disp

digitalWrite(segA, HIGH);

digitalWrite(segB, HIGH);

digitalWrite(segC, HIGH);

digitalWrite(segD, LOW);
digitalWrite(segE, LOW);

digitalWrite(segF, LOW);

digitalWrite(segG, LOW);
break;

case 8:// when count value is 8 show”8” on disp

digitalWrite(segA, HIGH);

digitalWrite(segB, HIGH);

digitalWrite(segC, HIGH);

digitalWrite(segD, HIGH);

digitalWrite(segE, HIGH);

digitalWrite(segF, HIGH);

digitalWrite(segG, HIGH);
break;

case 9:// when count value is 9 show”9” on disp

digitalWrite(segA, HIGH);

digitalWrite(segB, HIGH);

digitalWrite(segC, HIGH);

digitalWrite(segD, HIGH);

digitalWrite(segE, LOW);

digitalWrite(segF, HIGH);

digitalWrite(segG, HIGH);
break;
break;

}
while(!Serial.available());
COUNT = Serial.read()-48;
Serial.println(COUNT);
/*
if (COUNT<10)
{
COUNT++;
delay(1000);///increment count integer for every second
}
if (COUNT==10)
{
COUNT=0;// if count integer value is equal to 10, reset it to zero.
delay(1000);
}*/
}

Result:
We can read serial input and display it on a 7 segment display

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy