2

I'm at the tail end of a project using a Leonardo, a HC-06 bluetooth module and a few cell phone vibration motors. Everything is working fine except the serial communication between the Leonardo and the HC-06. I've done some tests to rule out common mistakes

  1. TX is connected to RX, and vice versa.
  2. Using a multimeter I checked for continuity with the RX TX, no problem there
  3. I can upload the script
  4. I uploaded the exact same script to an Uno, using the same HC-06. I was able to connect to the HC-06 with my phone and execute the script. So I know it's not the script and I know it's not the HC-06.
  5. The Leonardo is working properly because I can upload the script and execute it using the Arduino Serial Monitor.

The only clue I have is that I can upload the script with the RX/TX connected and that's not supposed to work. However, I checked the connections and there is continuity. So I'm stumped. The only thing I can think of is that I damaged the RX/TX while I was soldering, but I'm hoping it's not that. Is there a way to test for that. Any suggestions?

/*
 * created by Rui Santos, http://randomnerdtutorials.com
 * Bluetooth App - Turn On and Off LED
*/

#include <SoftwareSerial.h>
SoftwareSerial mySerial(1, 0); // RX, TX

int led = 3;
int state;

void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  if(Serial.available() > 0){     
      state = Serial.read();}

  if (state == '1') {
      digitalWrite(led, HIGH);}   // turn the LED on (HIGH is the voltage level)

  else if (state == '2') {
      digitalWrite(led, LOW);}    // turn the LED off by making the voltage LOW

  delay(100);               // wait for 100ms

  //For debugging purpose
  //Serial.println(state);
}
  • 1
    You should read the Leonardo manual. All will be explained. It's very different from an Uno. – Majenko Feb 9 '18 at 19:41
  • Why are you using software serial on pins 0 and 1? – gre_gor Feb 9 '18 at 19:44
  • On the Leo the RX and TX are labeled RX1 and TX0, so I assumed they were pins 0 and 1. But I also deleted that line of code and tested it and still it didn't work – Sarah M Feb 9 '18 at 19:47
3

An Arduino Leonardo has native USB support. Which means that it has separate serial interfaces to the PC and to the hardware pins.

Serial is only used for the communication with the PC and is unrelated to the hardware serial pins 0 and 1. Those are exposed trough Serial1.

So, if you are using the exact same Uno sketch, that uses the hardware serial pins, it can't work on a Leonardo, because data is just sent-to/read-from the PC and not to the connected Bluetooth module connected on pins 0 an 1.

  • Thanks. Any suggestions on how to change the script to reflect that difference? – Sarah M Feb 9 '18 at 20:00
  • You need to replace Serial with Serial1. – gre_gor Feb 9 '18 at 20:02
  • You're a genius gre_gor, it worked!! Thanks for the help, I needlessly banged my head against the wall for 3 days. Have a good one! – Sarah M Feb 9 '18 at 20:07

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.