1

I am trying to use an ultrasonic sensor in conjuncture with different devices like a proximity sensor and a solenoid and alarm.

This is how the project is supposed to work:

A container is placed on a fixture and the proximity sensor detects the placement of the container. Once the container is detected the ultrasonic sensor needs to be activated and the pulse time is measured and the distance is calculated. The container has unique features on each side and the aim of the ultrasonic sensor is to measure the distance and if the distance value is within the required spec then it should turn on a solenoid to allow the next operation or if it's outside the spec it should raise an alarm.

following is the code:

/*Objective of this project

1. To be able to detect when a container is placed in the wrong oreientation and cut of the supply to pneumatic system if so.
We will use the following devices to do so

1. Ultrasonic sensor to measure distance differences of both sides of the container
2. IR sensor to measure if the conatiner has been placed or not (This sensor will activate the ultra sonic sensor)
3. A soliend that will be controlled by the output of the ultrasonic sensor. The solenoid will control the air flow.

*/

int trigPin = 7;
int echoPin = 8;
int contSig = 2;
int solSig = 12;
int errSig = 13;
void setup() {
  
  Serial.begin(9600);
  pinMode(trigPin,OUTPUT); 
  pinMode(contSig,INPUT_PULLUP); 
  pinMode(echoPin,INPUT);
  pinMode(solSig,OUTPUT);
  pinMode(errSig,OUTPUT);

  }

void loop() {
  if (digitalRead(contSig) == 0){
    Serial.println(digitalRead(contSig));
    digitalWrite(trigPin,LOW);
    delayMicroseconds(10);
    digitalWrite(trigPin,HIGH);
    delayMicroseconds(10);
    int duration = pulseIn(echoPin,HIGH);
    Serial.println(duration);
    float dist = (duration/2) * 0.034;
    Serial.println(dist);
    if (dist >=20 && dist <= 60){
      digitalWrite(solSig,HIGH);
      digitalWrite(errSig,LOW);
    }
    else{
      digitalWrite(solSig,LOW);
      digitalWrite(errSig,HIGH);
    }
  }
    
    
}

I am facing a few issues.

1.The first If statement at the beginning does not seem to work properly.

2.The output on the serial monitor alternates between zeros and actual values. I am not sure why this set of zeroes keeps coming inbetween but this will make the project fail.

Following is the output from the serial monitor:

0

0

0.00

0

2892

49.16

I have been trying to simulate this on tinkercad

3
  • 1
    pulseIn returns zero if no pulse is detected within the default timeout period. The other zeros are ones that you print explicitly.
    – Majenko
    Mar 21 at 10:46
  • "The first If statement at the beginning does not seem to work properly" Why do you say that?
    – timemage
    Mar 21 at 12:41
  • I have simulated this program on tinker cad, I do not understand why the zeroes are still appearing. As per the logic, we should not be having zeroes. The if the state does not seem to be working because when I change the contSig value from 0 to 1 I still get a reading from the ultrasonic sensor which should not be happening Mar 24 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.