LilyPad and Bluetooth JY - MCU HC-06 module
I decided to make a post about this because it's something I have used many times in the past, but somehow every time I get my hands on one of these JY-MCU Bluetooth modules I seem to need to figure it all out from the beginning...
There are several tutorials out there about this module using Arduino UNO and other microcontrollers, but the most straightforward information about it comes from its datasheet: https://core-electronics.com.au/attachments/guides/Product-User-Guide-JY-MCU-Bluetooth-UART-R1-0.pdf Therefore, I strongly suggest reading through this before getting started.
I am going to take a starting point the tutorial described in the datasheet and apply it to the LilyPad Arduino 328 Simple Board (the connections between the module and the LilyPad are similar for all FTDI-type connectors e.g. LilyPad 328 Main, Arduino Pro Mini etc). If you follow the tutorial of the datasheet you can probably figure out easily how to connect the Bluetooth module to any micro controller - Flora, Circuit Playground, Teensy etc).
So, let's start from the example code. Upload the code below to your LilyPad Simple board by selecting the appropriate serial port in your computer. We need to upload the code first, since the serial pins we' re using to upload the code are later going to be connected to the Bluetooth module.
/* JY-MCU Bluetooth Wireless Serial Port module for Arduino® ON and OFF Control of a LED thru Bluetooth . The code example will switch ON or OFF on board LED by sending command ‘B’ or any other character thru the Bluetooth paired PC or Laptop. The pins used are designed are for JY-MCU Bluetooth Wireless Serial Port module with Arduino® Uno Board available from: https://core-electronics.com.au/store/index.php/wireless/bluetooth/jy-mcu-arduino-bluetooth-wireless-serial-port-module.html This example code is in the public domain. */ // Declaration of constants and variables to used by program char recd_dat; //variable for receiving data from bluetooth serial port int on_brd_led = 13; // On-board LED pin detail void setup() { // initialize the serial communications: //serial communication is used to receive the data from // Bluetooth module Serial.begin(9600); //Onboard LED pin as output pinMode(on_brd_led, OUTPUT); // The initial state of led is defined here // HIGH on PIN will switch on the LED // LOW on PIN will switch off the LED digitalWrite(on_brd_led, LOW); } void loop() { if (Serial.available() ) { // if serial data is available to read recd_dat = Serial.read(); //read data & store it in ‘recd_dat’ } if (recd_dat == 'B') { // if 'B' was received digitalWrite(on_brd_led, HIGH); // turn ON LED } else { digitalWrite(on_brd_led, LOW); // otherwise switch OFF } delay(150); // Just wait 15 0ms for next reading }
The connections between the Bluetooth's pins and the LilyPad 328 are pretty straightforward:
HC-06 pins LilyPad 328 pins
VCC -----------------------> +
GND -----------------------> -
TX -------------------------> RX
RX -------------------------> TX
According to many tutorials you can use the SoftwareSerial library to assign any pins you want as TX (transmit data) and RX (receive data) on your micro controller. But in this tutorial I' m going to keep it pretty simple and use the Arduino designated D0 (RX) and D1 (TX) pins of the ATmega328 - which in the LilyPad 328 Simple Board can be accessed through the FTDI connector.
That's it! If you' ve connected the pins correctly you should be able to turn on and off the built-in led of your LilyPad through the Bluetooth module. Just select the port where your Bluetooth is connected (mine for example is: /dev/cu.HC-06-DevB), open the Arduino Serial Monitor and type B and hit enter. The on-board led should light up! By sending any other key or command, the led should turn off.
No need to pair your computer with the Bluetooth device through your Bluetooth Preferences (at least on a Mac, can't talk about Windows)! Now it's time to move on and do something more creative with the wireless system you've created... I like to use Firmata and communicate with Processing or SuperCollider and have sensors controlling sound. The possibilities are endless!
Will be posting shortly about danceWare - a simple prototype I've been working on with an 3-axis accelerometer and a stretch sensor to be used in my upcoming collaboration with dancer and choreographer Stephanie Liapis. More info about it coming soon...!