Hello,
I am building a water tank sensor (based on this project: Wireless Water-Tank Level Meter with Alarm | Arduino Project Hub but without some of the features.)
The project uses a ultra sonic sensor AJ-SRO4M and a radio transmitter. The receiving unit has an lcd screen which displays the level of water in the tank. The project worked well with 1 sensor. However, I have 2 water tanks and I wanted to add a second sensor.
To test the transmitter, the readings were displayed in a serial monitor. The first sensor works fine but the second isn't reading. The results show 0cm. I already bought another sensor. I tested this new sensor, using the single sensor code and it worked.
I am using a atmega328. code is below:
/* Water tank sensor transmitter*//
#include <SoftwareSerial.h>
SoftwareSerial HC12(4, 5);
int POWER = 6;
long dist_1 = 0;
long dist_2a = 0;
long dist_2 = 0;
int TRIG1 = 2;
int ECHO1 = 3;
int TRIG2 = 8;
int ECHO2 = 7;
void setup() {
HC12.begin(9600);
Serial.begin(9600);
pinMode(TRIG1, OUTPUT);
digitalWrite(TRIG1, LOW);
pinMode(ECHO1, INPUT);
pinMode(POWER, OUTPUT);
digitalWrite(POWER , 1);
pinMode(TRIG2, OUTPUT);
digitalWrite(TRIG2, LOW);
pinMode(ECHO2, INPUT);
}
void loop() {
digitalWrite(TRIG1, LOW);
delayMicroseconds(5);
digitalWrite(TRIG1, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG1, LOW);
digitalWrite(TRIG2, LOW);
delayMicroseconds(5);
digitalWrite(TRIG2, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG2, LOW);
dist_1 = (pulseIn(ECHO1, HIGH))*0.034/2;
dist_2 = (pulseIn(ECHO2, HIGH))*0.034/2;
dist_2a = dist_2 * -1;
HC12.write(dist_1);
delay(500);
HC12.write(dist_2a);
delay(500);
Serial.print("TANK 1 ");
Serial.print(dist_1);
Serial.print("cm");
Serial.println();
Serial.print("TANK 2 ");
Serial.print(dist_2);
Serial.print("cm");
Serial.println();
}
HC12 is the radio transmitter. dist_2a is my way to distinguish the 2 readings in the receiving unit. The reading for tank 2 will be negative. (I am a very basic coder...)
Maybe there is an issue with using pins 7 and 8 for the trig and echo?
Any help would be appreciated.