int ledPin = 13; // use the built in LED on pin 13 of the Uno
int state = 0;
int flag = 0; // make sure that you return the state only once
void setup() {
// sets the pins as outputs:
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600); // Default connection rate for my BT module
}
void loop() {
//if some data is sent, read it and save it in the state variable
if(Serial.available() > 0){
state = Serial.read();
flag=0;
}
// if the state is 0 the led will turn off
if (state == '0') {
digitalWrite(ledPin, LOW);
if(flag == 0){
Serial.println("LED: off");
flag = 1;
}
}
// if the state is 1 the led will turn on
else if (state == '1') {
digitalWrite(ledPin, HIGH);
if(flag == 0){
Serial.println("LED: on");
flag = 1;
}
}
}
I used this code and when i connected the bluetooth module, the arduino and everything on the breadboard the motor would go off without any signal and when trying to connect to the bluetooth module the blinking red light doesnt turn to a solid color (as the documentation indicates)
The Arduino board led turning on or off when you send a 0 or 1 is sufficient to tell if the bluetooth link and program is working. It could be that the motor in addition to the bluetooth module is a too large drain on the voltage regulator of the Arduino, causing the entire setup to fail. But are you sure the bluetooth link is correctly established? It seems the link is not yet made if the red led on it is blinking. Read page 16 of the pdf datasheet I linked previously to see which sort of blinking determines the internal state of the bluetooth module.
ah91086:
Wait I don’t understand, I should test if it works by removing the wire connecting the cathode to the VCC
Yes, pull the diode leg of the cathode side out of the breadboard. That takes the motor out of the circuit. In your latest photo you also have the white wire going to the bluetooth module. That should still be connected to Vcc via the orange wire.
ah91086:
The data sheet indicates that a different led will light up when connected and that doesn’t happen so it’s not connected
If I look at the photo’s on Amazon I see a led near the State-pin hole. That one would blink with different intervals depending on the state of the module. Can you identify which state it is. The pdf manual shows different pulse patterns.
That’s ok, as the Arduino Pin 13 LED would still turn on telling you when a 1 was (succesfully) sent.
As the device is still not in the paired state, the motor or the led should not be able to change anyway.
It looks like the pairing and connecting with the bluetooth module isn’t working. Did you select the proper device to pair with on your phone? What name did you try to connect to. Did you supply the correct (pin) number as password?
ah91086:
im trying to connect it to my computer but to send it a 1 or a 0 i have to use a terminal, any idea which one i should use?
If you can setup the bluetooth dongle in your pc to communicate over a COM-port (I don’t know how to make that work on your pc), then the Arduino Serial Monitor should be able to work with that. You just have to select the proper COM-port number in the Tools menu and baudrate settings in the Serial Monitor window.
Disconnect the RX and TX wire from the Arduino. Instead connect the RX and TX of the bluetooth module together. Then anything that you send to it should be returned back. So you would receive a 1 or a 0 back if it was correctly sent. If this works then the bluetooth link is working correctly.