I am trying to control a led ON/OFF using Arduino UNO and a Bluetooth shield
I have this
enter image description here
enter image description here
enter image description here
which is based on hc-05. The tutorials I found about hc-05 are not working for me. I have the hc-05 embedded in the shield and I did not find any tutorials for this shield.
Here is my code (it is working when I use the USB serial ctrl + shift + m).
void setup () {
pinMode( 13, OUTPUT );
digitalWrite(13,LOW);
Serial.begin( 9600 );
Serial.println( "Ready" );
}
void loop() {
if ( Serial.available() > 0) {
int inByte = Serial.read();
switch ( inByte ) {
case '0':
digitalWrite( 13, LOW );
break;
case '1':
digitalWrite( 13, HIGH );
break;
}
}
}
I connected my android phone to the shield successfully using blurterm2. When I send 1 or 0 nothing happens. What am I missing?
-
If I understand your board the LED is on output 4. You should try to control it with simple on/off "hello world" logic before adding the serial input. If you connect the green lead to Vin does tshe LED light? If not, it is reversed or dead.Russell McMahon– Russell McMahon2015年07月10日 14:26:35 +00:00Commented Jul 10, 2015 at 14:26
-
i connected the led to pin 13, it is blinking when i control it from usb serail s10.postimg.org/izvhgzluh/20150710_173523.jpgAlexander– Alexander2015年07月10日 14:36:57 +00:00Commented Jul 10, 2015 at 14:36
4 Answers 4
Have you tried the turning just running a simple program to check if the led still works, such as
void setup () {
pinMode( 13, OUTPUT );
}
void loop() {
digitalWrite( 13, HIGH );
delay(1000);
digitalWrite(13,LOW);
delay(1000);
}
-
Yes the code you gave me is blinking the ledAlexander– Alexander2015年07月10日 15:03:42 +00:00Commented Jul 10, 2015 at 15:03
From your code I cannot see anything that actually initializes the shield and tells it what pins to use and how to use them. This github (https://github.com/jdunmire/HC05) has a library for the HC-05 shield and it appears to have instructions for setting baud rate and all of that good stuff.
Also, their read-me mentions that, if properly configured, you should see traffic to and from the shield in your serial monitor. Are you seeing any traffic?
NOTE: This tutorial (http://www.instructables.com/id/Arduino-AND-Bluetooth-HC-05-Connecting-easily/?ALLSTEPS) seems to have step by step instructions to do exactly what you are asking.
You should use a different baud rate if your hc-05 does not work with the code. The default is 9600. Use this baud rate and if not, try to find out the baud rate using AT commands, you will get several tutorials on this.
Here is your problem:
if ( Serial.available() > 0) {
int inByte = Serial.read();
You are not listening to the bluetooth-hardware, but to the build-in serial port of the Arduino.
I had a look at the url you can see on the board, but i could not find any example code. If you are lucky your bluetooth should might be compatible with the freetronics bluetooth shield, which has some example code on the bottom of this page to get you started:
#include <SoftwareSerial.h>
SoftwareSerial bt(2,3); // RX, TX
int thisByte = 33;
void setup() {
bt.begin(9600);
bt.println("ASCII Table ~ Character Map");
}
void loop() {
bt.write(thisByte);
bt.print(", dec: ");
bt.print(thisByte);
bt.print(", hex: ");
bt.print(thisByte, HEX);
bt.print(", oct: ");
bt.print(thisByte, OCT);
bt.print(", bin: ");
bt.println(thisByte, BIN);
if(thisByte == 126) {
thisByte = 32;
}
thisByte++;
}
This only sends, and "bt" is your bluetooth hardware here. It behaves like the Serial hardware for the most part.
Note: based on your picture, you might have to change the RX/TX pins on your board to 2,3 - currently they are set to 2,4.