HOW TO ENABLE PWM OUTPUT OF ULTRASONIC SENSOR WITH ARDUINO UNO

hello!

I'm a beginner in Arduino, and I have a ultrasonic sensor JSN-SR04t, and I'm using it to measure a volume of a reservoir and I tested the functionability of the sensor sending information to the Arduino APP, and everything is working fine, however I need to send the same information that I have in my computer (Arduino App) to a CLP, then I need to know how could be the insctrucion in the Arduino code to enable the output of the PWM of Arduino Uno.

Because of the CLP inlet tension is 24 Vcc, I will need to have a driver or something to increase up the voltage from 5 Vcc to 24 Vcc, but It I can solve later, what I really need now is to enable the PWM output with the proper code. The code that I'm using to my ultrasonic sensor to communicate with my Arduino APP (PC) I copied from internet and I did some modifications. Bellow follow the code:

obs.: the volu

#define trigPin 12
#define echoPin 11

float duration, volume;

void setup() {
Serial.begin (9600);
pinMode (trigPin,OUTPUT);
pinMode (echoPin,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// medindo o sensor ultrasônico

duration = pulseIn(echoPin, HIGH);

// determinar a distância a partir da duração
// velocidade do som 343 m/s

volume = ((49087.5 * (336 - (duration / 2) * 0.0343)) / 1000) * 1.2;

// enviando resultado para o Arduino

Serial.print("volume = ");
if (volume >= 20000 || volume <= 2) {
Serial.println("out of range");

}
else {
Serial.print(volume);
Serial.println(" litros");
delay(500);

}
delay(500);

}

The command to use PWM on the Arduino is analogWrite().

Ok.. but how I introduce it in my code, since I have a trigger pin and an echo pin?

You seem to have a volume range from 2 to 20000. How do you intend to communicate all of that via PWM? Or what is the PWM going to communicate?

Steve

Hello!...

I think if I have a proportional output related to the answer that I'm measuring by the sensor for me its good, for example zero liters been zero volts and 20000 Liters been 5 Volts, and the volumes in between could be proportional to the voltage in the range of 0 - 5 volts. If I have this kind of a response from the PWM output for me it's Ok!.... but I don't know how to do it. Do you have some idea?

but I don't know how to do it.

I don't think any one else has either, because what you are asking just dosn't make sense. An ultrasonic sensor does not have a PWM output, so enabling it dosn't make sense.

An ultrasonic transmitter sends out a short burst of waves. The receiver picks them up and you get information by timing the interval between sending and receiving.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.... :slight_smile:

Is there a reason you aren't just using the library for that sensor?

Hellow!

The library that I'm using works fine! What I really intend to do is to get the information of the level (volume) measured, from the Ultrasonic Sensor and read by the Arduino Uno, in a CLP, through Arduino Uno PWM Outputs, in order to have conditions to CLP to collect this informations, and record this information to create graphics, for example, volume x time, etc.

To accomplish it, I need to enable the PWM output(s) of the Arduino Uno, because the CLP accepts PWM inputs, however in a different voltage. So, what I don't know is how to do it... in the codes.... how to implement code lines to enable the PWMs output..

You use the analogWrite call. It takes two parameters, the PIN number and the duty cycle that goes from 0 to 255.
Note depending on the Arduino type only certain pins are capable of PWM. These are normally marked with a ~ symbol.

Di_Acqua:
Hellow!

**The library that I'm using works fine! **What I really intend to do is to get the information of the level (volume) measured, from the Ultrasonic Sensor and read by the Arduino Uno, in a CLP, through Arduino Uno PWM Outputs, in order to have conditions to CLP to collect this informations, and record this information to create graphics, for example, volume x time, etc.

To accomplish it, I need to enable the PWM output(s) of the Arduino Uno, because the CLP accepts PWM inputs, however in a different voltage. So, what I don't know is how to do it... in the codes.... how to implement code lines to enable the PWMs output..

What library?
What is CLP?
Can you post a link to data/spec of the "CLP"?

Thanks.. Tom.. :slight_smile:

consider the math and your output.

int pwm_litres = map(volume , 0 ,20000 , 0, 255) ; // scales input to 255 for output
// litres is just a name it is not really scaled to litres
int pwm_volume = volume/79; // 20,000 / 79 = 253.1645

Serial.print(" pwm_litres = ");
Serial.print(pwm_litres);

Serial.print(" - pwm_volume = ");
Serial.println(pwm_volume);

then as noted, you have to have picked the output pin to use analogwrite

analogwrite(pwm_litres); // litres scaled to something about 255
or
analogwrite(pwm_volume); // litres scaled to something about 255