Lab 5
Lab 5
Lab 5
Session 5:
Serial Communication and more
By
Eng. Mohamad Chamas
OBJECTIVES
• Serial Communication
• Switch Function
SESSION MATERIAL
• Serial Communication:
Serial communication in Arduino is a way for two devices to send and receive data to
each other serially, that is, bit by bit, over a communication channel. It's a fundamental
aspect of Arduino programming, enabling the microcontroller to communicate with
computers, sensors, and other microcontrollers. The Arduino platform supports
several forms of serial communication, including:
The Serial.begin() function in Arduino is used to initialize serial communication using the
UART (Universal Asynchronous Receiver/Transmitter) interface.
When you call Serial.begin(speed), you are setting up the UART interface with a specific
baud rate, which is the speed of communication in bits per second (bps). The speed
parameter specifies how fast the data is sent and received. Common baud rates include
9600, 115200, and others.
• RX (Receive Pin): This pin is used to receive data serially into the Arduino board.
• TX (Transmit Pin): This pin is used to transmit data serially from the Arduino board
to other devices.
When you connect an Arduino board to a computer using a USB cable, you are using the
USB interface for communication. However, the Arduino board often includes a USB-to-
UART bridge chip (such as the ATmega16U2 on the Arduino Uno or CH340 on some
clones). This bridge chip converts the USB communication from the computer into UART
signals that the main microcontroller on the Arduino can understand.
Here's what you need to consider when using pins 0 and 1 for digital I/O:
Serial Communication Interference: Using pins 0 and 1 for digital inputs or outputs can
disrupt serial communication, such as causing errors in the Serial Monitor or issues
uploading new sketches, because these pins are also used for serial connections.
Temporary Use: It's best to use Arduino's pins 0 and 1 for digital I/O only when
necessary, such as when additional pins are needed beyond initial programming, and
serial communication isn't in use. This is suitable for cases where extra input buttons are
required after the Arduino has booted up and is not using the serial port for
communication.
Reconnecting for Programming: If you have devices connected to pins 0 and 1 and
need to upload a new sketch to the Arduino, you might need to disconnect these devices
temporarily to ensure reliable serial communication for programming.
Optional: When you communicate over serial, data is transmitted as a series of bits (1s
and 0s). However, Serial.read() abstracts away the bit-level representation and returns the
data as a byte-sized integer For example:
• If the incoming data is the character 'a', Serial.read() will return 97, which is the
ASCII value for 'a'.
• If the incoming data is the character '1', Serial.read() will return 49, which is the
ASCII value for '1'.
When you're programming the Arduino and checking the value of incomingByte against
a character or a digit in your conditions, it's important to distinguish between comparing
against characters and comparing against numeric values.
Once you have uploaded the code you need to go to play store and download arduino
bluetooth controller app.
• Relay 1 channel:
A relay typically has five pins, three of which are high voltage terminals (NC, COM, and NO)
that connect to the device being controlled.
The device is connected between the COM (common) terminal and either the NC
(normally closed) or NO (normally open) terminal, depending on whether the device
should remain normally on or off.
Between the remaining two pins (coil1 and coil2) is a coil that acts as an electromagnet
Normally (initial position), the COM terminal is connected to the NC terminal and the NO
terminal is open.
When current flows through the coil, the electromagnet becomes energized, causing the
switch’s internal contact to move. The COM then connects to the NO terminal,
disconnecting from the NC terminal.
When the current stops flowing through the coil, the internal contact is returned to its
initial position, re-connecting the NC terminal to the COM and re-opening the NO
terminal.
1. Output Terminal Block
The high voltage terminals (NC, COM, and NO) of the relay are broken out to a screw
terminal. The device you want to control can be connected across them.
2. Control Pin
On the other side of the module, there is an input pin IN for controlling the relay. This pin
is 5V logic compatible, so if you have a microcontroller like an Arduino, you can drive a
relay with any digital output pin
The input pin is active low, which means that a logic LOW activates the relay and a
logic HIGH deactivates it.
3. Module Power
The module operates on 5 volts and draws approximately 70 mA when the relay is
activated.
The one-channel relay module is designed to allow your Arduino to control a single
high-powered device. It has a relay with a maximum current rating of 10A at 250VAC
or 30VDC.
When a break statement is omitted in a switch case, the program does not actually
check if the next case's condition is met. Instead, it simply continues executing the
code for the following case(s) as if they were part of the original case that matched.
This behavior is known as "fall-through."
HOMEWORK TASK: Bluetooth-Controlled Lamp
Objective: Develop a circuit that enables you to wirelessly control a lamp using an Arduino,
relay module, and HC-05 Bluetooth module. Your system should accept two different
character commands via Bluetooth: one to turn the lamp on and a separate one to turn it
off.
Materials Needed:
• Arduino Uno (or any compatible Arduino board)
• HC-05 Bluetooth module
• Relay module suitable for the Arduino (5V coil voltage)
• Lamp with a socket and plug
• Electrical wire
• Smartphone with Bluetooth capability
• Breadboard and jumper wires
• Resistors for voltage division
Safety Note: Please be cautious when dealing with mains electricity!!!!!!!!!! If you
are not experienced or certified to work with mains voltage, you may use a low-
voltage lamp for safety purposes. (12 Volt Adapter)