Bluetooth + HC-SR04 connection, something wrong with my wiring setup?

Hi,

I'm working on a personal project just to learn my way around Arduino, but I've recently hit a roadblock trying to connect the distance measuring HC-SR04 with my bluetooth connector (JY-MCU).

I've successfully created projects using them individually (using the HC-SR04 to measure distance, and using the JY-MCU bluetooth connector to control an LED), and now am trying to combine the projects.

The goal is to be able to turn the device on and off via bluetooth, and have the device display the distance of an object. In addition, the closer the object an RGB LED will change colours.

However, currently my code and setup simply results in the HC-SR04 returning 0, and I'm not entirely sure why. I have a little bit of schooling background in C, but no experience in regards to electronics and circuitry, so I believe my wiring is the reason why I'm having issues.

Thanks for any help or insight!

Here's the code I'm using:

const int redPin = 7;       //red led
const int greenPin = 6;     // green led
const int bluePin = 5;      // blue led
const int numReadings = 10; // # to average
const int trigPin = 11;     // Trigger
const int echoPin = 12;     // Echo
long duration, cm;

int readings[numReadings];    // array to store numbers from analog input
int readIndex = 0;            // index of current reading
int total = 0;                // running total of values (to calc average)
int average = 0;              // result

char blueToothVal;
char lastValue;
 
void setup() {
  //Serial Port begin
  Serial.begin (9600);
  //Define inputs and outputs
  
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  for (int i = 0; i < numReadings; i++){ //initialize array
    readings[i] = 0;
  }
}
 
void loop() {

  if(Serial.available()){
    blueToothVal = Serial.read();
  }

  if(blueToothVal == 'n'){
    //turn on ultrasonic sensor
    digitalWrite(trigPin, LOW);   
    delayMicroseconds(5);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    pinMode(echoPin, INPUT);
    duration = pulseIn(echoPin, HIGH);

    cm = (duration/2) / 29.1;     // convert duration to centimeters

    Serial.println(duration);  //DEBUG

    //*************calc average box******************
    total -= readings[readIndex];                   // subtracting the last reading
    readings[readIndex] = cm;                       // reading input from potentiometer 
    total += readings[readIndex];                   // add new reading
    readIndex++;                                    // increment counter
  
    if(readIndex >= numReadings){                   // when counter exceeds array, reset
      readIndex = 0;
    }
  
    average = total / numReadings;
    //**********************************************

    if(average <= 10){
      //red led (255 0 0)
      setColour(255,0, 0);
    }else if(average <= 20){
      //yellow led (255 255 0)
      setColour(255, 255, 0);
    }else if(average <= 30){
      //green led (0 255 0)
      setColour(0, 255, 0);
    }else{
      Serial.println(average);
    }
    lastValue = blueToothVal;
  } else if(blueToothVal == 'f'){
    //turn off
    digitalWrite(trigPin, LOW);
    setColour(0,0,0);
    lastValue = blueToothVal;
  } 
  delay(300);
}

void setColour(int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
}

And here's my wiring setup.

dfanger:
I've successfully created projects using them individually (using the HC-SR04 to measure distance, and using the JY-MCU bluetooth connector to control an LED), a....................I believe my wiring is the reason why I'm having issues.

If you have successfully created individual projects, why suspect the wiring? All you need is to duplicate the previous. Having said that, it is good practice to use a 1k/2k voltage divider in Arduino Tx line. The absence of this is not likely to be fatal.

Nick_Pyner:
If you have successfully created individual projects, why suspect the wiring? All you need is to duplicate the previous.Having said that, it is good practice to use a 1k/2k voltage divider in Arduino Tx line. The absence of this is not likelly to be fatal.

I am mainly suspecting my wiring because although I have successfully created previous works, a lot of the wiring advice came from tutorials I read. I've wired it up in a way that makes sense in my head, but without a concrete guide to follow this time I'm not sure if my logic translates correctly to reality.

As opposed to suspecting the code, most of it was working code from the ultrasonic sensor project which I'm sure works, the only addition is the RBG lighting, which is currently working. As it's the same code as the prior project where it functioned, I figured the problem was more likely to lie in the hardware.

Your wiring diagram does not show cross connection of the Tx/Rx (Tx>Rx Rx>Tx) between the HC05 and the Arduino HW Serial.

If you disconnect the bluetooth module , does your code work with usb and Serial monitor sending 'n' and 'f'?

cattledog:
Your wiring diagram does not show cross connection of the Tx/Rx (Tx>Rx Rx>Tx) between the HC05 and the Arduino HW Serial.

If you disconnect the bluetooth module , does your code work with usb and Serial monitor sending 'n' and 'f'?

Ah, the diagram is wrong but it was wired correctly, I've updated the picture to the correct diagram.

Upon taking your advice and stripping down everything to the basics (just connecting the HC SR04), turns out that it stopped working sometime between the last time I used it and now.. My previously working code with it now also just returns 0, as well as basic "copy/paste" code from tutorials.

I suppose I'll have to buy a new one..

Actually, on a better idea I got from a friend, he suggested I test all my wires and it turns out the two different wires I tried as my 5V connectors were both broken inside and didn't work.

So... that's a lesson learned. Thank god for multimeters.