Instructables

Arduino dual ultrasonic liquid level meter with integrated website

Step 2: The wireless XBee sensor

Picture of The wireless XBee sensor
DSC_0501.JPG
Since we had some 'barebone arduino'  (ATMEGA 328P-PU with bootloader) laying around, we choose to use this for the remote sensor.

Xbee's DIN and DOUT are connected to D0 and D1 on the arduino, so the build--in Serial can be used.

The sketch running on it is very simple, it reads out the URM37 (connected to pin 2 and 3), and transmits the distance in cm through serial to the Xbee.


int URPWM1=2;             // PWM Output 0-25000us,every 50us represent 1cm
int URTRIG1=3;             // PWM trigger pin
                                   
unsigned long urmTimer = 0;                          // timer for managing the sensor reading flash rate

unsigned int Distance1=0;


void setup(){                                      // Serial initialization
                            // Sets the baud rate to 9600
  PWM_Mode_Setup();
  pinMode(URTRIG1,OUTPUT);           
  Serial.begin(9600);
}

void loop(){  
   if(millis()-urmTimer>1000){
     urmTimer=millis();
     PWM_Mode();
   }
}

void PWM_Mode_Setup(){
  pinMode(URTRIG1,OUTPUT);                            // A low pull on pin COMP/TRIG
  digitalWrite(URTRIG1,HIGH);                         // Set to HIGH 
  pinMode(URPWM1, INPUT);                             // Sending Enable PWM mode command

}

void PWM_Mode(){                                     // a low pull on pin COMP/TRIG  triggering a sensor reading
    digitalWrite(URTRIG1, LOW);
    digitalWrite(URTRIG1, HIGH);                      // reading Pin PWM will output pulses
    unsigned long DistanceMeasured1=pulseIn(URPWM1,LOW);
  
   if(DistanceMeasured1<49000){                   
      digitalWrite(13,LOW);
      Distance1=DistanceMeasured1/50;                  // every 50us low level stands for 1cm
   } else {
     Distance1=0;
     digitalWrite(13,HIGH);
   }
  

  if ((Distance1 > 3) && (Distance1 < 400)) {
    Serial.print(Distance1);
    Serial.print("-");
  }
}
 
Remove these adsRemove these ads by Signing Up