Controlling a remote Arduino using a PC

The various Arduino Sketches and the controlling PC program can be found on my GitHub page here.

arduino-serial-remote-commander

 This PC program was written using Visual Basic 2010 on a Windows 7 machine, but the program has been tested on a Windows 10 machine, so it should work on most PCs. It does not install anything on the PC and can be run from a USB stick.

Warning – This is a long blog, but it can be summed up simply as 1. set up the Arduino using the Serial Option of your choice and upload the sketch. 2. Plug the Master into a PC running the control program 3. Control the Slave Arduino.

Overview:

The PC program monitors the five Analog Pins and the  Digital Pins D2 to D13 on a remote Arduino. The state of these pins is displayed and updated every 500 ms, the Digital OUTPUT pins can be switched with a button on the PC screen. Communication is via the Serial Port and at the PC end an Arduino is plugged into a USB socket, communication to the Slave Arduino is either by a direct connection, via Bluetooth using an HC05/06 or using an HC-12 433mHz radio. Distance for Bluetooth is about 30m in free air and although the HC-12 is quoted as having a range of 1800m I suspect that somewhere in excess of 300m in free air should be achievable. This project is designed to monitor and control a remote location (using solenoids or relays) and would not be suitable for use in the remote control of models due to the fairly slow update time.

The Slave Unit:

arduino-serial-remote-slave

Upload the Sketch Arduino_serial_Commander_Slave.ino before connecting the pins D0 and D1 (Rx, Tx) to the circuit.

The circuit diagram shows both Bluetooth and HC-12 radio connected, but in practice only one would be used. If the HC-12 is used then the Logic Level Converter can be left out as it is only needed for the HC05/06. The Serial Connection uses the Arduino’s hardware Serial Port leaving pins D2 to D13 free for control. Pin D13 is used to provide a Beacon function and should be left as an OUTPUT, but the other digital pins can be set up as either INPUTS or OUTPUTS by modifying the sketch

// ————– Pin variables
int command;
int pin13;int pin12;int pin11;int pin10;int pin9;int pin8;
int pin7;int pin6;int pin5;int pin4;int pin3;int pin2;
//
// ————– setup digital pins as OUTPUT or INPUT,
// OUTPUT = 0 (Zero), INTPUT = 1 (One)
//
int pin2Direction = 0;
int pin3Direction = 0;
int pin4Direction = 0;
int pin5Direction = 1; // INPUT
int pin6Direction = 0;
int pin7Direction = 1; // INPUT
int pin8Direction = 0;
int pin9Direction = 0;
int pin10Direction = 0;
int pin11Direction = 0;
int pin12Direction = 0;
int pin13Direction = 0;
//

The sketch sets all digital pins as an OUPUT except for D5 and D7, which are set as INPUTS, but this is easy to alter to your requirements. For an INPUT set the pinDirection for the required pin to 1 and for an OUTPUT set it to 0 (zero).

In the Main Loop a timer sends a string to the Master containing the state of all the pins every 500ms. It listens for data from the Master and if any is available reads it

if(Serial.available() > 0){ // get data sent from Master
String input = Serial.readString();
command = input.toInt(); // convert to an integer

the result, command, is a single number that represents the state of all the digital pins displayed on the PC screen. If you imagine this number in hexadecimal format then 0x0800 represents D13 and 0x0001 represents D2. So, if the number received was 0x0801 then pins D13 and D1 would be set HIGH and all the other pins set LOW. If the next command received was 0x0800 then pin D13 would be left HIGH but pin D2 would be set LOW. This method allows all pins to be controlled by using a single number.

That’s it for the main loop, a subroutine sendDatatoMaster() builds a string containing the state of the Analog and Digital pins to send back to the Master, when requested every 500ms. I settled on 500ms after some experiment as it provides the most reliable connection and trying to send the data at a faster rate led to a large number of failures. No checksum is used because any error will be corrected after the next transmission in 500ms. The Slave transmits data without knowing if the Master has received it, however, if the Beacon is switched ON from the PC then the LED attached to D13 will flash every 2 seconds all the time contact is maintained between the Master and the Slave.

The Master Unit:

arduino-serial-remote-master

Upload the sketch Arduino_serial_Commander_Master.ino to this Arduino.

The wiring of the Master unit is almost identical to the Slave unit except that Software Serial is used on pins D2 (Rx) and pin D3 (Tx) and an additional connection from pin D4 to the HC-12 (if used) SET pin. This allows an HC-12 to be put into COMMAND Mode. As with the Slave Unit, only one Serial method at a time can be used so the wiring can be simplified to suit your needs. The Master Arduino is plugged into a USB port on the PC.

This Arduino does nothing much other than to take the Serial output from the PC and send it to the Slave. Any data it receives it sends back to the PC to be decoded and acted on. It does however, look for two special commands that allow an HC-12 radio to be put into Command mode and this is highlighted in bold below

if(Serial.available() > 0){ // Read from serial input (from PC) and send over HC-12
String input = Serial.readString();
//Serial.println(input); // for debugging
if(input.startsWith(“ATEND”)){
digitalWrite(ATpin, HIGH); // exit AT Mode
digitalWrite(LEDpin, LOW);
}
else if(input.startsWith(“ATSEND”)){
digitalWrite(ATpin, LOW); // go into AT Mode
digitalWrite(LEDpin, HIGH);
}
else{
mySerial.println(input);
}
}

 Clicking the AT Command Mode button will allow AT commands to be sent to the HC-12 in the Master Unit (only). Clicking the button again will return to the Normal mode.

Setting up the Serial Mode:

Before starting make sure the program HC12 Commander.exe is running on a PC and the correct Arduino Sketch has been uploaded to each Arduino.

Mode 1 – Hardware Serial Data Mode

This is perhaps the best mode to start with to make sure everything is working.  Plug the Master Arduino into a USB port on the PC and supply power to the Slave Arduino first. Without either a Bluetooth Unit or HC-12 Radio connected to either the Master or the Slave connect the Tx on the Master to the Rx on the Slave, connect the Rx on the Master to the Tx on the Slave. Connect the Gnd pins on both Arduino Boards together.  (Note before switching off the power to either Arduino remove the Serial and Ground connections first).

With the PC program on your monitor click on the Serial Port Radio Button

commander-serial_port_button

Click on the Baud Rate drop down box and select 9600

commander-baud_rate

Then click on the COM Port drop down box, the COM Ports connected will be shown, select the one that matches your Arduino

commander-comm_port

After a few seconds, the Received Data String box will show that the Master has connected to the Slave, the Serial Port symbol will turn blue and the pin states on the Slave will be shown. As the Slave sketch set up D5 and D7 as inputs their buttons are greyed out. If they are not connected they will normally show HIGH, try connecting pin D7 to pin D8, the value will change to LOW. Click on D8 to change it to HIGH and D7 will go HIGH. The pins that are HIGH are shown on the Arduino picture in red, the onboard LED connected to D13 will turn red when D13 is HIGH. As the update occurs only once every 500ms there will be a slight delay.

commander-connected

Click on the Beacon Off button the title changes to Beacon On and after a slight delay, the onboard LED on the Slave will start to flash once every 2 seconds. This will continue until either the Beacon button is pressed again or the Slave loses contact. Other digital pins can be changed while the Beacon is running.

Now disconnect the three Serial wires after a short delay the Digital Pin display will be greyed out and the Received Data Strings box will show that contact has been lost. The Master then tries to link up with the Slave and will keep doing this until contact is made. The Serial Port icon turns black.

commander-lost

The Beacon LED on the Slave will stop flashing but, remain in the state it was in when contact was lost. Reconnect the three serial wires and after a few moments, the Received Data Strings box will show that contact has been made. The Digital Pins display will become active and if the beacon was running before contact was lost the LED on the Slave will start to flash again.

 The Analog pins tend to float if not connected and the values shown will be random, try connecting one of the Analog pins to a Digital pin. If the digital pin is LOW the Analog pin will be 0, if the digital pin is HIGH the output will be 1024.

The Same procedure is used with Bluetooth and with an HC-12 radio with some minor variations.

Mode 2  – HC-12 433mHz Radio mode:

Connect an HC-12 to the Master Arduino and to the Slave Arduino (do not connect the Serial connection used above), Make sure Tx on the HC-12 connects to Rx on the Arduino and Rx on the HC-12 connects to Tx on the Arduino. Connect the Master Arduino to the PC and connect power to the Slave. Separate the two units by a few feet and go through the procedure to connect as shown in Serial Mode above. This time click on the HC-12 Radio Button before setting the Baud Rate at 9600 and the COM Port to your Arduino. The AT Mode now becomes useable and the Data Strings to Send box and associated buttons become enabled.

In the normal mode, the HC-12 mode acts exactly as the Hardware Serial Data Mode, but if you click on the AT Command Mod Off button the title changes to AT Command Mode ON and the HC-12 is put into Command Mode. Type in AT (upper or lower case) and click the SEND button. OK should be displayed in the Received Data Strings box.

commander-at

Try typing AT+RX and press SEND all the settings on the HC-12 will be shown, use the scroll bars to the right of the box to see earlier data.

Click on the AT Command Mode ON button, it will change to AT Command Mode OFF and you will be back in Normal Mode. Please Note that this cannot be used to set up AT mode on an HC05/06 Bluetooth module.

Mode 3 – Bluetooth HC05/06 Mode:

Bluetooth can be used in two ways, either by using the Arduino Master fitted with a Bluetooth module, or not using the Master and using the Bluetooth on the PC or a Bluetooth dongle instead.

Method 1 – using the Master Arduino fitted  with an HC05 or HC06

The HCo6 is a Bluetooth Slave module and the HC05 is a Master/Slave module. Before starting the HC05 should be configured as a Master and it should be confirmed that the two modules will pair. It is beyond the scope of this blog to cover how this is done, but there is plenty of information on the internet. Fit the HC05 to the Master Arduino and the HC06 to the Slave board (although it does not matter which Arduino the HC05 is connected to). Plug the Master into a USB socket on the PC and run the control program. Select the Bluetooth radio button. Apply power to the Slave board and the LED on both Bluetooth boards will flash. After a few seconds, the LEDs on both Bluetooth LEDs will stop flashing and stay ON, showing that they have paired. Select a Baud Rate of 9600 and select the COM Port for the Master Arduino. After a short delay, the two Arduino’s will connect and the Digital pin display will become enabled.

Method 2 – Using the PC’s Bluetooth instead of a Master Arduino.

If the PC has a Bluetooth or you have fitted a Bluetooth Dongle you can dispense with the Master Arduino. You first need to know which port your Bluetooth is connected to. Click on the Bluetooth Icon in the taskbar and click on Show Bluetooth Devices

commander-bt2

If you have already paired the HC05/06 then it will appear

commander-bt1

If not then click on Add a Device, when asked use the password 1234. Once it has installed the drivers (this may take a few minutes) right click on the device (HC-06 in the picture above) and click on properties to show the following menu screen. Click on Services, this shows the port we want to be COM 110

commander-bt3

Now start the PC program with Bluetooth running. Select the Bluetooth Radio Button. Set the baud rate to 9600 and the COM Port to that found (COM 110) and with the slave powered up, Bluetooth should connect in a short while, the LED on the HCo6 going from flashing to steady.

Advertisements

Remote Bluetooth Thermometer with OLED Display.

Part 2 describing the Receiver is here.

The code for this project can be downloaded from my GitHub page here.

This is a version of an earlier post I made, using HC05 and HC06 Bluetooth modules to make a wireless thermometer. This project uses an OLED Display on the receiver.

This post details the construction and setting up of the transmitter. I have used an Arduino Uno, but a Mega or even Nano could be used. The HC05 can be powered from 3 to 5 volts, but the Tx and Rx lines are 3 volts only. A logic level converter, like this one from Hobbytronics, which has a 250ma 3volt regulator that can be used to power the HC05.

Parts List:
Arduino Uno
HC05 Master/Slave Bluetooth
Bidirectional Logic Level Shifter
TMP36 thermometer

bluetooth transmitter

The connection diagram is shown above, connect a flying lead to the KEY pin unless your breakout board has a small push button fitted. This will be used to switch the HC05 into command mode.
Once the circuit has been built upload the hc05_command_mode.ino sketch. Connect the KEY pin to 3.3 volts (or press the small push button if one is fitted) and apply power, Once the sketch is running remove the connection to the KEY pin (or release the small push button if one is fitted). The HC05 should now be in command mode, running at 38400 baud, check this by opening the Serial Monitor, (set to 9600 and NL/CR) and type in AT and click SEND, OK should be shown.

To set the HC05 up to act as a Master Unit, with it in the command mode enter the following commands into the Serial Monitor, pressing SEND each time.

AT+ORGL this resets the HC05 to factory settings
AT+ROLE =1  this sets the unit to Master
AT+INQM=1,9,48. this allows up to 9 Bluetooth units to be able to connect
AT+CMODE =1  this sets the connection mode to allow any unit to connect
AT+UART=9600,0,0 this sets the baud rate to 9600

Once these commands have been entered remove the power. Now build the receiver.

 

JMYCU Bluetooth units, HC05 and HC06 Part 3 – linking two Arduino’s 

Part 1 setting up the HC06 Slave Bluetooth unit is here.

Part 2. Setting up the HC05 Master/Slave unit is here.

The Universal Bluetooth Receiver with LCD display is here.

Important note: if the Key pin on the HC05 is HIGH when the power is connected it will enter th AT Mode using a baud rate of 38400. As the Slave is set at 9600 baud this presents problems. However, if the Key pin is taken HIGH after power is applied the baud rate is unchanged. In the sketch used here the Key pin is connected to Arduino pin 4. This pin is taken HIGH shortly after power is applied. The HC05 will enter AT mode at 9600 baud rate and is set as a Master Unit, then paired with the HC06. The Key pin can be left HIGH and still the HC05 will be able to communicate with the HC06 as long as the Key pin was taken high after the power was applied.

Once the two Bluetooth units are working the next thing is to link the Master with the Slave. It is useful to use the Universal Bluetooth Receiver with LCD display and the link for this is shown above. 

We now need to apply power to the Arduino/Slave then load the next sketch into the Arduino/Master unit, this will allow AT commands to be sent to the HC05.

With both Arduinos powered On type the following commands into the Serial Monitor connected to th Master make sure NL/CR has been selected.

AT+INIT (only do this once , after applying the power)
AT+INQM=0,9,30 (this will search for up to nine Bluetooth units)
AT+ROLE=1 (sets the HC05 as a master, it will not search unless this is done)
AT+INQ (prints out the address of nearby Bluetooth units)

The bluetooth addresses of nearby units will now be displayed. The address is the first three groups separated by a comma, there are two other groups of numbers.

My HC06 gave 13,1B,412154 as the address.

Once you have identified the HC06 on the Slave Arduino type

AT+LINK=13,1B,412154 (inset your address here)

to link to the HC06. Remember to make sure LF and CR are selected in the serial monitor, or send \r\n with each command. Once linked the LED on the HC06 will stop flashing and remain lit.


/*******************************************************
// HC05 Master connects to HC06 Slave
//
// On first running the Bluetooth module is reaset to factory settings then the speed is set to 9600
//
// Connect RX from the HC05 to pin 2 on the Arduino.
// Connect TX from the HC05 to pin 3 on the Arduino

// to send AT commands KEY is connected to Vcc
// after power is connected, to keep the baud rate at 9600
// by controling the Key pin with Arduino pin 4
// Switch the power on
// Open up the Serial Port Monitor
// Type in AT and click on the SEND button, OK will be printed in the monitor
// showing that the module is receiving commands.
//
*****************************************************/

#include <softwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
#define keyPin 4 // connects to HC05 KEY
String buildString = ""; // Stores response of bluetooth device
// which simply allows \n between each response.

void setup()
{
// Open serial communications and wait for port to open:
// Hardware Serial port set to 9600
Serial.begin(9600);
// SoftwareSerial "com port" data rate. 38400.
mySerial.begin(9600); // default speed in AT mode
//
pinMode(keyPin, OUTPUT);
digitalWrite(keyPin, LOW); // puts HC05 into communication mode
//
delay(1000);
digitalWrite(keyPin, HIGH); // put HC05 into AT mode
delay(200);
//
// reset bluetooth module to factory settings
// Note: the delay blocks are necessary to allow the commands to take place

// this will take about 25 seconds to link up with HC06
Serial.println("Initialising this will take about 25 seconds...");
mySerial.write("AT+ORGL\r\n");
delay(2000);
mySerial.write("AT+UART=9600,0,0\r\n");
delay(2000);
mySerial.write("AT+NAME=BluetoothBee\r\n");
delay(4000);
mySerial.write("AT+INIT\r\n");
delay(4000);
mySerial.write("AT+INQM=0,9,30\r\n");
delay(4000);
mySerial.write("AT+ROLE=1\r\n");
delay(4000);
mySerial.write("AT+INQ\r\n");
delay(4000);
mySerial.write("AT+LINK=13,1,41254\r\n");
delay(2000);
Serial.println("HC-05 set as a Master, trying to connect to slave");
}

void loop() {
mySerial.write("testing...");
delay(1500);
mySerial.write("connection");
delay(1500);
}

As far as I can determine the HC05 in Master mode will only connect with an HC06. The TinyOS Bluetooth Shield uses an HC05 ( see this post here) and so will also connect with an HC 06 and can use all of the code shown above.

JYMCU Bluetooth Units, HC-05 and HC-06 Part 1

 
Part 2 shows setting up the Master HC05 and can be seen here.

It is important to note that although the HC05 and HC06 can be powered from 5 volts the control pins and T/Rx  pins must be 3 volts. Use a logic level converter, an example is shown later. I power the device from the 3 volt output on the logic level converter.

A Piconet is a collection of devices connected via Bluetooth technology in an ad hoc fashion. A piconet starts with two connected devices, such as a portable PC and a mobile phone. All Bluetooth devices are peer units and have identical implementations. However, when establishing a piconet, one unit will act as a master for synchonization purposes, and the other unit will be slave for the duration of the piconet connection.
The JYMCU Bluetooth units come in two varieties, one set permanently as a Slave, the HC-06  and the other that can either be a Slave or a Master, the HC-05. The two units look identical, but the Master unit is usually more expensive. Normally if you are using Bluetooth to connect to an Android device, or PC, then a Slave unit is all you need. If you want to communicate between two Arduino’s then one Adruino can use a Slave unit, but the second Aduino will have to use a Master unit. Up to eight Bluetooth units can exist in a Piconet.

The JYMC can be powered from 5volts but a Logic Level Converter must be used on the Tx/Rx pins. The Bluetooth acts as a Serial Communication device and to ensure the Arduino’s Tx and Rx (pins 0 and 1) are free to upload Sketches. I use SoftwareSerial. To test Bluetooth out I have installed BlueTerm on my Android Phone, the phone then acts as a Master unit and the Arduino acts as a slave. Data can be entered into the Serial Monitor on the Arduino and it will appear on the mobile phone, and the mobile phone or android device can switch the onboard LED on the Arduino ON or OFF. 

Upload the following sketch to an Arduino and connect Gnd and Vcc on the JYMC to Gnd and 5volts on the Arduino. Use a Logic Level Shifter (you can purchase one fromHobbytronics  here for £3. The advantage of this board is that it has a 3.3 volt 250 mA regulator) to connect Tx on the Bluetooth to the SoftwareSerial Rx pin and Rx on the Bluetooth to the SoftwareSerial Tx pin.


/*****************************************************
Simple Send/Recieve test SoftwareSerial for Bluetooth HC06
Sends data from Serial Monitor and will turn LED on pin 13
ON if the character 1 is sent
OFF if the character 0 is sent
ingnores all other data

Vcc to either 3 volts or 5 volts
Gnd to Arduino Gnd
Tx to Arduino pin 10
Rx to Arduino pin 11

Unless this has been altered the bluetooth unit will be found as 'Linvor'

***************************************************************/

#include <SoftwareSerial.h>
#define onboardLed 13

SoftwareSerial mySerial(10, 11); // RX, TX

void setup()
{
Serial.begin(9600); // Open serial communications and wait for port to open:
mySerial.begin(9600); // SoftwareSerial "com port" data rate. JY-MCU v1.05 defaults to 9600.
pinMode(onboardLed, OUTPUT);
digitalWrite(onboardLed, LOW); // turn off onboard LED
}

void loop()
{
// Read device output if available.
if (mySerial.available()) {
while(mySerial.available()) { // While there is more to be read, keep reading.
unsigned char charreceived = mySerial.read();
Serial.println(charreceived); // show data from bluetooth
switch(charreceived){
case 49:
digitalWrite(onboardLed, HIGH);
Serial.println("Arduino Led On");
break;
case 48:
digitalWrite(onboardLed, LOW);
Serial.println("Arduino Led Off");
break;
default:
break;
}

}
}
// Read user input if available.
if (Serial.available()){
delay(10); // The DELAY!
mySerial.write(Serial.read());
}
}// END loop()


Start the Arduino and switch to the Serial Monitor, start BlueTerm on your Mobile Phone and go to Connect Devices. Click on the Scan for Devices button, you should find the HC05 as Linvor. The red LED on the Bluetooth unit will stop flashing and light continuously. Type something into the input box on the Serial Monitor ( set to 9600 baud and send CR/LF selected) and it should appear on your mobile phone. Now type the number 1 in the BlueTerm terminal screen, the onboard LED will light, type 0 and the LED will go out. All other characters will be ignored.

If nothing seems to be working check that the Tx and Rx pins from the Bluetooth are connected correctly. If you can connect to the HC05 with your mobile phone running BlueTerm then it must be a connection problem. These units run at 9600 baud.

  

This is the basic circuit, when mounted on a baseboard additional circuitry is added such as a 3 volt regulator and a USB interface.

AT Commands:

When the HC-06 is first supplied with power the LED blinks and AT commands can be sent to it. Once the HC-06 connects to a Master Bluetooth unit it switches to communication mode. The HC 06 has a limited number of AT commands and they are described here. These commands must be sent within 1 second and should not have a CR/LF added.


Default settings of the HC-06:
Slave
9600, N, 8, 1
Name linvor
Pincode 1234

Command test:
Sent: AT
Receive: OK

Change baud rate:
Sent: AT+BAUD#
Receive: OKbaud
1: 1200
2: 2400
3: 4800
4: 9600
5: 19200
6: 38400
7: 57600
8: 115200

Example:

Sent: AT+BAUD1
Receive: OK1200

Change device name:

Sent: AT+NAMEname
Receive: OKname

Example:

Sent: AT+NAMEblue
Receive: OKblue

Change pin code:

Sent: AT+PINxxxx
Receive: OKsetpin

Example:

Sent: AT+PIN9999
Receive: OKsetpin