This lesson is a simple tutorial explaning how to control Arduino UNO Ports using Computer's Bluetooth. Bluetooth is a type of wireless communication used to transmit voice and data at high speeds using waves of radio. It’s widely used. This type of communication is a cheap and easy way to control something remotely using arduino.
Figure 1 - LED Control Circuit
HC Bluetooth Module
HC Bluetooth module has 4 pins to be connected to arduino, they are:
- RXD : RXD will receive data from arduino
- TXD : TXD will send data to arduino
- VCC : VCC is the power supply (3.3V 6.6V)
- GND : GND is the ground
Figure 2 - HC 07 Bluetooth Module
On my module, as you can see from the photo above, the TX line is rated at 3.3V. This means that even though we can power the module with 5 volts from the Arduino, the communication lines from and to the module are supposed to be at 3.3 volts. Sending data from the Bluetooth module (via the module’s TX pin) will not be an issue, as the Arduino’s RX line will interpret the 3.3V signal from the Bluetooth module correctly. Receiving data from the Arduino is where we need to do more work. The RX line of the Arduino Uno is running at 5V, so we will be sending a higher voltage on the Bluetooth’s module RX line, than suggested by the label. This may damage the Bluetooth module permanently!
You have to pay attention about the RXD level, some modules work with 5V, but this one works with 3.3V, and arduino TX will send a 5V signal, then it needs a voltage divider.
The formula above we have R1 = 1,2 K R2 = 2,2 K Vin = 5v.
Solving for Vout we get: Vout = 5 x 2,2 K / (2,2 K + 1,2 K ) = 5v x 2,2 K / 3,4 K = 3.24 V
Essentially, you can use any combination of resistors, as long as R2 is twice the value of R1.
If you do not have resistors handy, you can use a positive 3.3v voltage regulator. Connect the Input pin of the voltage regulator to the Arduino TX line, the Ground pin to the Arduino Ground and the output pin of the voltage regulator to the JY-MCU RX line.
Figure 3 - The connection circuit of Arduino UNO and HC 07 Bluetooth Module
ARDUINO UNO Program
/*C# ile Bluetooth üzerinden LED Kontrolü (The LED Control via Bluetooth with C#)
http://www.bilisimkitabi.net
http://bilisimkitabi.blogspot.com.tr
*/
// LED çıkışlarını tanımla (Define LED OUTPUT)
int LED1=2;
int LED2=3;
int LED3=4;
int LED4=5;
int chr=0;
void setup()
{
Serial.begin(9600);//Seri habarleşme başlatılır (Start Serial Communication)
//LED ler için Portları tanımla (Define Digital Port for LEDs)
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(LED4,OUTPUT);
}
void loop()
{
if(Serial.available())
{
chr= Serial.read(); //Bluetooth üzerinden gelen bilgiyi oku (Read the data via Bluetooth)
if(chr==1)
{
digitalWrite (LED1, HIGH);
}
else if(chr==2)
{
digitalWrite (LED1, LOW);
}
else if(chr==3)
{
digitalWrite (LED2, HIGH);
}
else if(chr==4)
{
digitalWrite (LED2, LOW);
}
else if(chr==5)
{
digitalWrite (LED3, HIGH);
}
else if(chr==6)
{
digitalWrite (LED3, LOW);
}
else if(chr==7)
{
digitalWrite (LED4, HIGH);
}
else if(chr==8)
{
digitalWrite (LED4, LOW);
}
}
}
LED Control With Bluetooth Program with C# 2013
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;////EKLE
(You Have to add this line)
namespace LEDControlBluetooth
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int l1 = 0, l2 = 0, l3 = 0, l4 =
0;
private void Form1_Load(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();//Seri Portu Oku (Read Serial
Port)
foreach (string port in ports)
{
comboBox1.Items.Add(port); //Listeye At (Add List)
}
}
private void button1_Click(object sender, EventArgs e)
{
string x = comboBox1.SelectedItem.ToString();
serialPort1.PortName = x;
serialPort1.BaudRate = 9600;
serialPort1.Open();
}
private void button2_Click(object sender, EventArgs e)
{
try
{
if (!serialPort1.IsOpen)
serialPort1.Open(); //Seri portu aç
}
catch
{
MessageBox.Show("Seri Port Seçin!");
}
}
private void button3_Click(object sender, EventArgs e)
{
serialPort1.Close(); //Seri Portu Kapat
}
private void button4_Click(object sender, EventArgs e)
{
if
(serialPort1.IsOpen == true)
{
if (l1 == 0)
{
byte[] chr = new byte[] { 1 };
serialPort1.Write(chr, 0,
1); //Seri Porta 1
Karakterini gönder
//(Write 1 char to serial port)
l1++;
}
else
{
byte[] chr = new byte[] { 2 };
serialPort1.Write(chr, 0, 1);
l1--;
}
}
else
// gerçekleşmemişse
hata mesajı verir.
{
MessageBox.Show ("Cihaz Bağlı Değil");
}
}
private void button5_Click(object sender, EventArgs e)
{
if
(serialPort1.IsOpen == true)
{
if (l2 == 0)
{
byte[] chr = new byte[] { 3 };
serialPort1.Write(chr, 0, 1);
l2++;
}
else
{
byte[] chr = new byte[] { 4};
serialPort1.Write(chr, 0,
1);
l2--;
}
}
else
// gerçekleşmemişse
hata mesajı verir.
{
MessageBox.Show("Cihaz Bağlı Değil");
}
}
private void button6_Click(object sender, EventArgs e)
{
if
(serialPort1.IsOpen == true)
{
if (l3 == 0)
{
byte[] chr = new byte[] { 5 };
serialPort1.Write(chr,
0,1);
l3++;
}
else
{
byte[] chr = new byte[] {6 };
serialPort1.Write(chr, 0,
1);
l3--;
}
}
else
// gerçekleşmemişse
hata mesajı verir.
{
MessageBox.Show("Cihaz Bağlı Değil");
}
}
private void button7_Click(object sender, EventArgs e)
{
if
(serialPort1.IsOpen == true)
{
if (l4 == 0)
{
byte[] chr = new byte[] { 7};
serialPort1.Write(chr, 0,
1);
l4++;
}
else
{
byte[] chr = new byte[] { 8 };
serialPort1.Write(chr, 0,
1);
l4--;
}
}
else
// gerçekleşmemişse
hata mesajı verir.
{
MessageBox.Show("Cihaz Bağlı Değil");
}
}
}
}
Do not forget to type " using System.IO.Ports;" when writing the program. This library provides the serialPort object to work.
Project FilesYou can download project files by clickin the links.
Hiç yorum yok:
Yorum Gönder