No Wire or Wireless? - Part 1
by AZDelivery in Circuits > Arduino
3325 Views, 9 Favorites, 0 Comments
No Wire or Wireless? - Part 1
This is part one in our series “Wireless communication devices for Arduino and Raspberry Pi”. There will be four parts. First will be about bluetooth module HC-05, second will be about RF radio transmitter and receiver 433MHz module and third will be about NFR24L01 module and fourth will be about ESP8266-01 module. So for the first part, we will write about bluetooth communication, HC-05 module, how to write Android app using MIT App Inventor for bluetooth, and how to pair two HC-05 modules.
So let’s start.
Parts list for this is:
- two Arduino Nano with CH340
- two Bluetooth modules HC-05
- logic level converter 3.3V - 5V
- two breadboards
- few jumper wires
- a button
- an LED
- one 220 Ohm resistor
- one 1k Ohm resistor
- potentiometer
In our store, we also have Arduino Nano with FTDI chip, so choose one you like, it does not matter which one, both do the job. We also have free Quick Starter Guide for Nano, and free Quick Starter Guide for logic level converter.
So what is a bluetooth? Well, it is no tooth, and there is nothing blue in bluetooth, except a legend about Viking king called Harald Bluetooth on which bluetooth communication get its name. You can google about history of bluetooth name, it’s very cool. For example, Wikipedia article, Name and Logo section.
Bluetooth is a communication protocol, set of rules for wireless communication using short-wavelength radio waves from 2,4GHz to 2,485GHz. It divide this frequency band into 79 different channels, and then uses these channels to send or receive data. It creates a secure wireless connection for exchanging data amongst several different devices. Bluetooth protocol has several classes for many usages, for example class 2 is used in mobile phones with range of around 10 meters. Bluetooth protocol is master/slave, which means, that for communication we have master device which communicate with one or max 7 slave devices. Master device can be connected to only one slave device at the time, but bluetooth protocol enables quick switches between multiple slave devices, so that it seems that master device is connected to all of them.
For HC-05 module what we should know, is that it can be used as Bluetooth Serial Port and that it can work in both master and slave modes. We already wrote about Serial communication in our instructable. So with bluetooth module we will use nothing more. This module is actually two boards connected in one. The smaller green one, is actual bluetooth module, and it uses 3.3V for power and logic. The bigger blue one, is attachment board and it has 3.3V power regulator, so that we can power whole module with 5V, BUT logic is still on 3.3V (RX and TX pins), so that is why we need logic level converter with this module, or voltage divider (but we won’t cover it here).
Logic level converter is a device that converts 5V to 3.3V or vice versa. You can choose any you like. We used Sparkfun bidirectional logic level converter and here is connection diagram.
Let’s first make a sketch for our Arduino, and than an app for our Android phone using MIT App Inventor. Then we will establish communication between the two, and use Android phone to turn on built in LED on our Arduino.
Sketch for Nano
So, let’s first make a sketch for our Arduino. Copy and paste this sketch to your Arduino IDE:
#define ledPin 13 int state = 0; int flag = 0; void setup() { pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); Serial.begin(9600); } void loop() { if(Serial.available() > 0) { state = 0; state = Serial.read(); flag = 0; // Serial.println(state); // For debug }</p><p> if (flag == 0 && state == 1) { digitalWrite(ledPin, HIGH); Serial.println("LED: ON"); flag = 1; } if (flag == 0 && state == 0) { digitalWrite(ledPin, LOW); Serial.println("LED: OFF"); flag = 1; } }
Nothing new here (we already used everything in our previous Instructable posts), but let’s quickly go through it. We define ledPin macro for our built in LED. You can use any other pin, but you have to connect external LED with resistor for it. Then we define two variables, state and flag. State is used for controlling LED, and when it is 1 > LED ON, and when it is 0 > LED OFF. Flag variable is used for controlling program flow.
In setup() function we define our ledPin as an output and drive it low, so that it is turned off at the beginning, and setup a serial communication to 9600 baud rate. This serial communication is used for wireless communication between Nano and Android phone, via bluetooth module. So here we see usage of bluetooth module as wireless serial port.
In loop() function we wait for data in serial communication if(Serial.available() > 0) and then read the data, and put it in state variable. After this we check for 0 or 1 and for example if state is equal to 1 than we turn on LED and send back to serial line message LED: ON. This is sent to bluetooth module and then to Android phone as message.
Making App in MIT App Inventor
This is not Instructable for android development, so we will quickly go through this, you have to experiment on your own if you wish to create different apps. We will just show you end product, and explain few key features. We will make seperate Instructable post for this in future.
To create app in MIT App Inventor go to this site and register user account. Then press a button Create Apps!. New page will open. Then click on Start new project, and name a project to for example Ardu_Bluetooth. Then new page will open (here we have end product, but for you it will be empty screen on phone layout). From menus on left pick layouts and move Horizontal Arrangement to empty screen in the middle. Create several Horizontal Arrangements like here. Adjust these things for all Horizontal Arrangements except one between two buttons. Put in few labels, buttons, and listPicker for bluetooth list devices. Change names for these items like here. For labels, make these options for bluetooth_status label, and these options for LED_status. For buttons make these options. And for listPicker, change the text to Bluetooth (scroll down, you'll find Text field).
Now, we need bluetooth client, and clock. These are non-visible items, and won't be seen on screen.
We made layout, now we have to program an app. To do so, click on Blocks button on right top corner of site. New window will open, this is end product but yours will be empty. We have 5 parts for this, first two are for listPicker which we named BluetoothList, second two are for two buttons, and last one is for clock, and setting the status of bluetooth connection.
To make a block, you just click on item on the left list, for example BluetoothList and pick one block in list. If something is related with bluetooth, you go to BluetoothClient1 and find block there.
For first BluetoothList items, first is called “when BluetoothList.BeforePicking do” here we go to list of bluetooth devices already paired with our Android phone, and pick one. In second BluetoothList item, “when BluetoothList.AfterPicking do” we check if there is a paired device, and if it is, we connect app to it.
For clock block, we pick “when Clock1.Timer do” block, and in it we put two if-then blocks. When devices are paired, this change status of bluetooth_status label to “Connected” and color text in blue. When they are not paired, this change status of bluetooth_status label to “Not Connected” and color text in red.
For both buttons we create almost the same two blocks, the differences are the names of blocks and in what is sent to Arduino. For example for TurnON_button e use block named “when TurnON_button.Click do” and in it we first send one byte of data, a number 1 (0 for TurnOFF_button) and then check for text that will be printed back. This text is set inside led_status label. There are few errors with this, because we need to press buttons twice in order to set this led_status label correctly. You can see this on gif video at the beginning of next step.
To build app go to Build App ( Provide QR code for .apk ). This will build app and provide QR code for downloading .apk file. You have to download any app from Google Store that can scan QR codes. QR code is a download link for .apk. When you download it, install it on your Android phone.
Here is actually app for those who don’t want to make it.
Downloads
How to Use App
Before using app, we have to pair Android phone with blutooth module. To do this, turn on bluetooth on your Android phone and power up Bluetooth module. Search for devices on Andorid phone, and when you see name "HC-05", connect to it. Password for this is “1234”.
Then start Ardu_Bluetooth app, and click on Bluetooth button. New window will open, with list of paired devices. If you only paired your Arduino phone with bluetooth module, one item will be in list. Click on it. Than bluetooth status label will be changed from red “Not Connected” to blue “Connected”. And than you can press on Turn ON or Turn OFF to turn on/off bulit in led on your Nano board.
Pairing Two HC-05 - AT Commands
Connect two Nanos and two bluetooth modules like on connection diagram. Make two schematics, one for master device, and one for slave device.
To pair two HC-05 modules, we have to use AT commands (ATtention commands). There are many of them, but we will cover just few. First we have to write a sketch in order to communicate with module. Copy and paste this sketch to your Arduino IDE (just uncomment first #include, because Instractuble don't show it uncommented):
/* BluetoothATmode * * Uses hardware serial to talk to the host computer * and Software Serial for communication with the bluetooth module * Text entered in the Serial Monitor is sent to the connected bluetooth module * Anything received from the connected module is printed back to the Serial Monitor * * Pins: * Bluetooth module > Arduino * VCC > 5V * GND > GND * TXD > Digital pin 8 (Software Serial Transmit - TX) * RXD > Digital pin 9 (Software Serial Receive - RX) * * Because bluetooth module logic work on 3.3V we need logic level converter for RX and TX lines * */ #include // <SoftwareSerial.h> SoftwareSerial SWserial(8, 9); // RX, TX< char character = ' '; boolean userInput = true; void setup() { Serial.begin(9600); SWserial.begin(38400); Serial.println("Software Serial started at 38400"); Serial.println(); } void loop() { // Read text from the Bluetooth module and send to the Arduino Serial Monitor if (SWserial.available()) { character = SWserial.read(); Serial.write(character); } // Read text from the Arduino Serial Monitor and send it to the Bluetooth module if (Serial.available()) { character = Serial.read(); SWserial.write(character); // Echo the user input to the Arduino Serial Monitor. // The ">" character indicates the user entered text. if (userInput) { Serial.print(">"); userInput = false; } Serial.write(character); if (character == 10) { // new line ASCII character \n userInput = true; } } }
In order for this to work, we have to power down bluetooth module and connect EN pin to 5V and than to power up bluetooth module. With this we put bluetooth module in AT mode, and now we can communicate with it. There is one red LED on bluetooth module and if it blink on every 2 seconds, bluetooth module is in AT mode.
Let’s explain the sketch a little bit. First we define software serial on pins 8 and 9 (8 > RX and 9 > TX). Then we setup hardware serial to 9600 baud rate, and software serial to 38400 baud rate (default baud rate of bluetooth module).
In loop() function we listen for messages on software serial, and if there is message on software serial, it will be printed to hardware serial. Than we listen for messages on hardware serial (User Input). These messages contain our input for AT commands.
Now, upload sketch from above to your Nano, one for master device and one for slave device (it is the same for both devices, but commands are not) and start Serial monitor.
AT commands: To enter a command simply write it in send text field of Serial Monitor, and send it.
- “AT” this is test command, and if everything is fine, return message will be “OK”
- “AT+UART?” will return something like “+UART:9600,0,0” and this 9600 is baud rate on which bluetooth module works.
- “AT+ROLE?” will return a role of module, “+ROLE:0” for slave device, and “+ROLE:1” for master device.
- “AT+ADDR?” will return an address of module, something like “+ADDR:98D3:31:F9472B” . We need to know this address of slave module in order to establish communication.
- “AT+ROLE=1” this 1 can be 0 or 1, 0 will set role to slave (default value), and 1 will set role to master. return message should be “OK” if everything worked ok.
- “AT+CMODE=0” tells master module to connect to fixed address.
- “AT+BIND=98D3,31,F9472B” tells master module to connect to slave device with address we determined above. Here we write commas, instead of colons.
So, here is two configuration history windows for master and slave module. You enter AT commands one by one, like in configuration histories. Notice on right bottom corner that we setup “Both NW & CR” and baud rate to 9600 (hardware serial).
Pairing HC-05 Sketches
Connect everything like on connection diagram for master device, and connection diagram for slave device. Here we have master Nano with bluetooth module, and an led connected to pin 3, and button connected to pin 2. For slave device, we have potentiometer which is actually variable voltage divider connected to pin ADC7.
Upload this sketch to master Nano with bluetooth module (just uncomment first #inclued):
#include // <SoftwareSerial.h> SoftwareSerial SWserial(8, 9); // RX, TX int state = 0; void setup() { pinMode(3, OUTPUT); pinMode(2, INPUT); Serial.begin(9600); SWserial.begin(9600); } void loop() { int toSend = digitalRead(2); // Serial.println(toSend); // For debugging SWserial.write(toSend); delay(10); if(SWserial.available() > 0){ state = 0; state = SWserial.read(); } Serial.println(state); analogWrite(3, state); }
Upload this sketch to slave Nano with bluetooth module (just uncomment first #inclued):
#include // <SoftwareSerial.h> SoftwareSerial SWserial(8, 9); // RX, TX int state = 0; void setup() { pinMode(LED_BUILTIN, OUTPUT); Serial.begin(9600); SWserial.begin(9600); } void loop() { int sensorValue = analogRead(A7); int toSend = map(sensorValue, 0, 1023, 0, 255); // Serial.println(toPrint); // For debugging SWserial.write(toSend); delay(10); if(SWserial.available() > 0) { state = SWserial.read(); // Serial.println(state); // For debugging } if (state == 0) { digitalWrite(LED_BUILTIN, LOW); } if (state == 1) { digitalWrite(LED_BUILTIN, HIGH); } }
Both sketches are self explanatory so we won’t go through them.
if you want to stay up to date with our next Instructable posts, just hit Follow button.
This is all for this Instructable.
If you have any questions, feel free to ask.
You can reach us under info@az-delivery.com at any time and we will be glad to help you, or visit us under www.az-delivery.com