Week 3: write a program to transfer data by using Ir sensor
IR Sensor: An infrared (IR) sensor is an electronic device that measures and detects infrared
radiation in its surrounding environment.
Hardware requirements:
Pin diagram:
GND------------GND
OUT--------------8
VCC------------------3.3 OR 5V
Procedure:
Program:
int IRPin =8;
int led=13;
int value;
void setup(){
pinMode(IRPin,INPUT);
Serial.begin(9600);
pinMode(led,OUTPUT);
void loop(){
value = digitalRead(IRPin);
Serial.println(value);
if(digitalRead(IRPin)==0)
digitalWrite(led,HIGH);
Serial.println("object detected");
else
digitalWrite(led,LOW);
Serial.println("object not detected");
Output:
3.b) Ultrasonic Sensor
Ultrasonic Sensor Defination: An ultrasonic sensor is an instrument that measures the distance to an object using ultrasonic sound
waves. An ultrasonic sensor uses a transducer to send and receive ultrasonic pulses that relay back information about an object's proximity
Pin diagram:
Source code:
#define ECHOPIN 7 // Pin to receive echo pulse
#define TRIGPIN 8
int led=12;
int a,b;
void setup()
Serial.begin(9600);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
pinMode(led,OUTPUT);
void loop()
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2000);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(1000);
digitalWrite(TRIGPIN, LOW);
float a = pulseIn(ECHOPIN, HIGH);
digitalWrite(led,HIGH);
b= a*0.0344/2;
Serial.print(b);
Serial.println(" cm");
delay(3000);
Output:
3. c) Write a program to transfer sensor data to a smartphone using Bluetooth on Arduino
Pin diagram:
#include <SoftwareSerial.h>
SoftwareSerial Bluetooth(8, 9); // RX, TX
int LED = 12; // the on-board LED
int Data; // the data received
void setup() {
Bluetooth.begin(9600);
Serial.begin(9600);
Serial.println("Waiting for command...");
Bluetooth.println("Send 1 to turn on the LED. Send 0 to turn Off");
pinMode(LED,OUTPUT);
void loop() {
if (Bluetooth.available()){ //wait for data received
Data=Bluetooth.read();
if(Data=='1'){
digitalWrite(LED,HIGH);
Serial.println("LED On!");
Bluetooth.println("LED On!");
}
else if(Data=='0'){
digitalWrite(LED,LOW);
Serial.println("LED Off!");
Bluetooth.println("LED Off ! ");
else{;}
delay(1000);
Output:
4.a) Write a program to implemrnt using arduino
Hardware requirements:
Pin diagram:
RFID Theory :
SPI Protocol
SPI, or Serial Peripheral Interface, is a serial communication protocol often
used in embedded systems for high-speed data exchanges between devices on
the bus. It operates using a master-slave paradigm that includes at least four
signals: a clock (SCLK), a master output/slave input (MOSI), a master
input/slave output (MISO), and a slave select (SS) signal. The SCLK, MOSI,
and MISO signals are shared by all devices on the bus. The SCLK signal is
generated by the master device for synchronization, while the MOSI and
MISO lines used for data exchange. Additionally, each slave device added to
the bus has its own SS line. The master pulls low on a slave's SS line to select
a device for communication.
A PICC (Proximity Integrated Circuit Card) is a standardized model of a
card that serves as a reference device for compliance testing of commercial
readers and acts as a model for commercial card's ICs (Integrated Circuits)
during antenna coil optimization.
RFID reader RC522 has a reading range of approximately 1 meter.
PCD(Proximity Coupling device): Also known as RFID readers. They decode
the RFID Tags and communicate with them based on ISO14443 standard. PCD
can perform read and write operation of data.
The communication between a PCD (Proximity Coupling Device; aka a
reader) and a PICC (= Proximity Integrated Circuit Card) is performed
using an electromagnetic field coupling between the reader and the card.
The PCD ensures the generation of the magnetic field whereas the antenna
of PICC allows receiving the magnetic field.
MFRC522
Model MF522-ED
Working frequency 13.56MHz
Card reading distance 0-60mm
Protocol SPI
Data communication speed 10Mbit/s Max
Understanding UART
UART, or Universal Asynchronous Receiver/ Transmitter, is a physical
circuit in a microcontroller or single integrated circuit (IC) that is used to
implement serial communication between devices in an embedded system.
Essentially, a UART’s main purpose is to transmit and receive serial data.
In UART communication, two UARTs communicate directly with each
other; the UART on the sender device, or the transmitting UART, receives
parallel data from the CPU (microprocessor or microcontroller) and
converts it to serial data. This serial data is transmitted to the UART on the
receiver device, or the receiving UART. The receiving UART converts the
received serial data back to parallel data and sends it to the CPU. In order
for UART to convert serial-to-parallel and parallel-to-serial data, shift
registers on the transmitting and receiving UART are used.
In UART communication, only two wires are required for communication: data
flows from the Tx pin of the transmitting UART (Transmitter Tx) to the Rx pin
of the receiving UART (Receiver Rx).
UART data is sent over the bus in the form of a packet. A packet consists of
a start bit, data frame, a parity bit, and stop bits. The parity bit is used as
an error check mechanism to help ensure data integrity.
4 B) RFID read information
Source code:
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9
#define SS_PIN 10
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println(F("Read personal data"));
}
void loop() {
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++)
key.keyByte[i] = 0xFF;
byte block;
byte len;
MFRC522::StatusCode status;
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
Serial.println(F("**Card Detected:**"));
mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid));
Serial.print(F("Name: "));
block = 4;
len = 18;
byte buffer2[18];
block = 1;
status =
mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1,
&key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Authentication failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
status = mfrc522.MIFARE_Read(block, buffer2, &len);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Reading failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
for (uint8_t i = 0; i < 16; i++) {
Serial.write(buffer2[i]);
Serial.println(F("\n**End Reading**\n"));
delay(1000); //change value if you want to read cards faster
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
4.c) Write information
Source code:
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9
#define SS_PIN 10
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println(F("Write personal data on a MIFARE PICC "));
void loop() {
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++)
key.keyByte[i] = 0xFF;
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
Serial.print(F("Card UID:"));
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.print(F(" PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
byte buffer[34];
byte block;
MFRC522::StatusCode status;
byte len;
Serial.setTimeout(20000L) ;
// Ask personal data: First name
Serial.println(F("Type First name, ending with #"));
len = Serial.readBytesUntil('#', (char *) buffer, 20) ;
for (byte i = len; i < 20; i++) buffer[i] = ' ';
block = 1;
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block,
&key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
status = mfrc522.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else Serial.println(F("MIFARE_Write() success: "));
block = 2;
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block,
&key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
status = mfrc522.MIFARE_Write(block, &buffer[16], 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
else Serial.println(F("MIFARE_Write() success: "));
Serial.println(" ");
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
5. Write a program to moniter tempereature and humidity using arduino.
Source code:
#include <DHT.h>
#define DHTPIN 8
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println(F("DHT test!"));
dht.begin();
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t))
Serial.println(F("Failed to read from DHT sensor!"));
return;
Serial.print(F(" Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);