0% found this document useful (0 votes)
51 views

Arduino Programming Code

This document discusses using an Arduino and Bluetooth module to control an LED from an Android device. It includes the components needed, wiring diagram, code explanation, and overview of a simple Android app to send on/off commands via Bluetooth. Custom characters can also be displayed on an LCD screen connected to an Arduino.

Uploaded by

Anam Badar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Arduino Programming Code

This document discusses using an Arduino and Bluetooth module to control an LED from an Android device. It includes the components needed, wiring diagram, code explanation, and overview of a simple Android app to send on/off commands via Bluetooth. Custom characters can also be displayed on an LCD screen connected to an Arduino.

Uploaded by

Anam Badar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Led on off onUART

Arduino Serial Communication using UART

Int
led=11;
char ch;

int onCount = 0;
int offCount = 0;

char myget();

void setup() {
Serial.begin(9600);
pinMode(led,OUTPUT);

}
void loop() {

ch = myget();
Serial.print(ch);
if(ch == 'o')
{
onCount = 1;
offCount = 1;
}
else if(ch == 'n')
{
if(onCount == 1)
onCount = 2;
else
onCount = 0;
}
else
{
onCount=0;
}
if (ch == 'f')
{
if(offCount==1)
offCount = 2;
else if(offCount==2)
offCount = 3;
else
offCount=0;
}

if(onCount == 2)
{
digitalWrite(led, HIGH);
// onCount=0;
}
if(offCount == 3)
{
digitalWrite(led, LOW);
// offCount=0;
}
}
char myget()
{
char ch = 0;
while
(Serial.available()==0);
ch=Serial.read();
return ch;
}
hc05_led_brightness_control

char ch[3]={1,2,3};
int data;
int i=0;
bool flag = true;
void setup() {
Serial.begin(9600);
pinMode(11,OUTPUT);

}
void loop()
{
if (Serial.available())
{
ch[i]=Serial.read();
i++;
flag = true;
}
else if (flag)
{
data=atoi(ch);
Serial.println(data);
analogWrite(11,data);
i=0;
memset(ch,0,3);
flag=false;
delay(1000);
}
delay(1000);
}

COMPONENTS AND SUPPLIES


Arduino UNO

× 1
LED (generic)

× 1

Resistor 221 ohm

× 1

Android device

× 1

HC-05 Bluetooth Module

× 1

Let’s Start Building


The circuit is so simple and small, there are only a few connections to be made
Arduino Pins           Bluetooth Pins
RX (Pin 0)     ———->      TX
TX (Pin 1)      ———->      RX
5V                ———->      VCC
GND             ———->      GND
Connect a LED negative to GND of Arduino and positive to pin 13 with a resistance valued
between 220Ω – 1KΩ. And you're done with the circuit

Note: Don’t  Connect RX to RX and TX to TX of Bluetooth to Arduino you will receive


no data, Here TX means Transmit and RX means Receive
How Does it Work?
HC 05/06 works on serial communication.here the android app is designed to send serial data to
the Bluetooth module when a certain button is pressed. The Bluetooth module at the other end
receives the data and sends it to Arduino through the TX pin of the Bluetooth module(RX pin of
Arduino). The Code fed to Arduino checks the received data and compares it.If received data is 1
the LED turns on turns OFF when received data is 0
Open the serial monitor and watch the received data
Android Application

How to use the App?


 Download the Application form playstore
 Pair your device with HC 05/06 Bluetooth module1) Turn ON HC 05/06 Bluetooth module2) Scan
for available device3) Pair to HC 05/06 by entering default password 1234 OR 0000
 Install  LED application on your android device
 Open the Application
splash screen

 Press paired devices


 Select your Bluetooth module from the List (HC 05)
Paring screen
 After connecting successfully
 Press ON button to turn ON LED and OFF button to turn OFF the LED
 Disconnect button to disconnect from Bluetooth module
This is just a basic tutorial on interfacing Bluetooth module with Arduino This project can be
improved to a higher level like Home automation using a smartphone, Smartphone controlled
robot, and much more.
Write data on sd card/save
data in sd card

#include <SPI.h>
#include <SD.h>
File myFile;
char buffer[] = {"EPT eck burjul quwait palazh Koh-e-noor
Town Faisalabad"};
void setup()
{
Serial.begin(9600);
SD.begin(10);
myFile = SD.open("test.txt", FILE_WRITE);
// write to it:
Serial.print("mydaaaaataa...");
myFile.println(buffer);
myFile.close();
Serial.println("done.");

// re-open the file for reading:


myFile = SD.open("test.txt");
Serial.println("test.txt:");
// read from the file until the line ends
String data = myFile.readString();
Serial.println(buffer);
int x = Serial.readBytesUntil("\n",buffer,80);
Serial.println(x);

myFile.close();
}

void loop()
{
}

Read data from sd card


#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}
Lcd display customcharacter

#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,4); // set the LCD address


to 0x27 for a 16 chars and 2 line display

void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.begin(16, 4);
}
void loop()
{
lcd.setCursor(8,0);
lcd.print("EPT eck burjul quwait palazh kohenoor fsd");
lcd.scrollDisplayLeft();
delay(1000);
}

You might also like