1705Views7Replies

Author Options:

How would I go about changing the frequency of the analogWrite() function in Arduino? Answered

I am designing an ultrasonic parking sensor, and need to use the analogWrite() function to produce a 50% duty cycle PWM.

I would ordinarily use the tone() function in this situation however, this is currently tied up with something else and noTone() cannot be called.

The problem I am having is with the default frequency of the analogWrite() function aliasing occurs with my reading on the ultrasonic sensor and I receive values that are a factor of ten off what they should be. I have tested the sensor at a range of frequencies and it appears to work best at about 50 Hz.

Any help would be greatly appreciated.

Comments

The forums are retiring in 2021 and are now closed for new topics and comments.
0
Pash1987
Pash1987

Answer 9 years ago

Yeah, 50Hz isn't very ultrasonic, but I've got a component to do that for me. All I need to do is give it a pulse to initiate the sensor.

Thanks for the link. I haven't fully read it yet, but it seems to be what I'm after.

0
steveastrouk
steveastrouk

Answer 9 years ago

NOT the old "polaroid" system, or its newer incarnation ?

Steve

0
Pash1987
Pash1987

Answer 9 years ago

Sorry, I'm quite new to this. I'm not quite sure what you mean, but if its the sensor you're on about, its an SRF04 ultrasonic ranger - http://www.robotstorehk.com/srf04tech.pdf

0
steveastrouk
steveastrouk

Answer 9 years ago

Ah, its a descendant of the Polaroid system.

I'm not sure why you need a 50Hz square wave to drive it though.

Steve

0
Pash1987
Pash1987

Answer 9 years ago

That's a very good point. I think I was just in a one-track fame of mind.
Although I guess it's useful to know how to alter the PWM frequency :)

0
steveastrouk
steveastrouk

Answer 9 years ago

Its also important to dynamically adjust the trigger depending on how long the last pulse took....

Steve