0% found this document useful (0 votes)
18 views19 pages

Arduino Bluetooth LED Control 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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views19 pages

Arduino Bluetooth LED Control 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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Led on off onUART

Arduino Serial Communication using UART

Int
led=11;
char ch;

int onCount = 0;
int offCount = 0;

char myget();

void setup() {
[Link](9600);
pinMode(led,OUTPUT);

}
void loop() {

ch = myget();
[Link](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
([Link]()==0);
ch=[Link]();
return ch;
}
hc05_led_brightness_control

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

}
void loop()
{
if ([Link]())
{
ch[i]=[Link]();
i++;
flag = true;
}
else if (flag)
{
data=atoi(ch);
[Link](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 [Link] 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 [Link] 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()
{
[Link](9600);
[Link](10);
myFile = [Link]("[Link]", FILE_WRITE);
// write to it:
[Link]("mydaaaaataa...");
[Link](buffer);
[Link]();
[Link]("done.");

// re-open the file for reading:


myFile = [Link]("[Link]");
[Link]("[Link]:");
// read from the file until the line ends
String data = [Link]();
[Link](buffer);
int x = [Link]("\n",buffer,80);
[Link](x);

[Link]();
}

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:
[Link](9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
port only
}
[Link]("Initializing SD card...");
if (![Link](10)) {
[Link]("initialization failed!");
while (1);
}
[Link]("initialization done.");
// open the file for reading:
myFile = [Link]("[Link]");
if (myFile) {
[Link]("[Link]:");
// read from the file until there's nothing else in it:
while ([Link]()) {
[Link]([Link]());
}
// close the file:
[Link]();
} else {
// if the file didn't open, print an error:
[Link]("error opening [Link]");
}
}
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()
{
[Link](); // initialize the lcd
[Link]();
[Link](16, 4);
}
void loop()
{
[Link](8,0);
[Link]("EPT eck burjul quwait palazh kohenoor fsd");
[Link]();
delay(1000);
}

Common questions

Powered by AI

Using a simple circuit to control LED lighting via smartphone commands showcases the feasibility of integrating microcontrollers with common communication modules for remote control tasks. This approach offers basic on/off control through Bluetooth, enhancing convenience and accessibility. Potential enhancements include expanding to full home automation systems, integrating with WiFi for extended range, adding sensors for automated responses, and expanding the control to other appliances, leading to more comprehensive smart home solutions .

Conditional structures like `if`, `else if`, and `else`, as well as switch-case statements, are vital in Arduino sketch programming for making decisions based on input conditions. They manage logic flow by executing specific code blocks only when certain conditions hold true, enabling selective response to inputs or states. This contributes to device functionality by allowing fine control over actions, such as toggling LEDs, processing serial commands, or managing data flows based on sensor inputs or user commands, enhancing device versatility and capability .

Delay functions in Arduino (`delay()`) pause program execution for a specified duration, which can simplify timing operations in loop structures. They are strategically used to manage the timing of events, reduce the frequency of data polling, or debounce button inputs. However, excessive use can degrade system performance by preventing the Arduino from executing other tasks or responding to external input, leading to sluggish responses. Alternatives like non-blocking delays (`millis()`) or interrupt-driven designs are often preferred for responsive user interaction .

In Arduino setups, components like LEDs and SD cards are integrated through GPIO pins and the SPI interface, respectively. For LEDs, digital pins output high or low signals to toggle LED states or PWM signals for brightness control. For SD cards, the SPI interface facilitates data flow by serial data transfer. The setup initializes peripherals (e.g., `SD.begin()` for SD cards) in the setup function, followed by coded logic in the loop for operational tasks. Data flow involves reading inputs (serial commands), processing them in Arduino, and driving outputs (LED states, writing data to SD cards) accordingly .

To pair an Android device with an HC-05 Bluetooth module, first turn on the Bluetooth module, then scan for available devices on the Android device. Once the HC-05 appears, select it and enter the default password (usually 1234 or 0000) to complete the pairing. After successfully connecting, an Android application can be used to send specific data to the Arduino via the Bluetooth module. Pressing an 'ON' button in the app sends data that turns the LED on, whereas an 'OFF' button turns it off .

The serial communication between the Arduino and the HC-05 Bluetooth module uses the TX and RX pins to transmit and receive data, respectively. The HC-05 module operates over a serial interface, sending data via its TX pin to the RX pin of the Arduino. Conversely, data from the Arduino is sent through its TX pin to the RX pin on the Bluetooth module. This bidirectional data exchange allows for commands to be sent from, for example, an Android device to control the Arduino, such as turning an LED on or off .

In Arduino, memory clearing is commonly achieved through functions such as `memset()`, which clears a memory block by setting all bytes to a specified value, often zero. In handling serial data, `memset()` is used to reset buffers that store incoming data after every cycle of reading, ensuring that previous data does not interfere with new inputs. This is crucial in processing commands correctly, as it guarantees that residual bytes do not corrupt new command processing .

Custom characters on an LCD connected to Arduino are created by defining a bit pattern for each character. The LiquidCrystal_I2C library, used for I2C communication with LCDs, initializes the display with `lcd.init()` and `lcd.begin()`. Backlighting is managed by calling `lcd.backlight()`, ensuring the display is visible even in low light conditions. Once set up, characters or strings can be displayed using `lcd.print()`, and their position can be controlled using `lcd.setCursor()`. This setup allows for dynamic on-screen characters and texts .

The `analogWrite()` function is used to control the brightness of an LED by changing the duty cycle of the PWM signal applied to the LED. This function outputs a voltage between 0V and 5V, simulating an analog output by rapidly oscillating between ON and OFF states. In the given setup, the brightness level is transmitted via the Serial Monitor and parsed into an integer before being applied to the LED. This interaction demonstrates real-time control over LED brightness through serial input .

Writing data to an SD card using Arduino involves initializing the SD card using `SD.begin()` and then opening a file in write mode with `SD.open()`. Data is written into this file using functions like `myFile.println()` and closed after writing. To read data, the file is opened in read mode, and functions such as `myFile.readString()` or `myFile.read()` iterate over the file contents. SPI library plays a crucial role in interfacing with the SD card as it provides the low-level communication protocol needed to send and receive data between the Arduino and the SD card .

You might also like