Frequency output problem

Hi .

I have written code using PWM.h library.
I read analog voltage at A1 pin of uno and vary output frequency at pin 9 of uno from 0HZ to 50HZ .
But when i turned on arduino and analogoutput is not connected it starts giving a fixed output frequency at pin 9 which i am using as output.

I want that when i power up my device it should give no output and only give output when a give analog volt ant pin A1.

I am using an external voltage source for analog input and also i have tried connecting a 1kOhms resistance across Analog input and ground.

Please help in this matter

Also let me know how to follow up my question on this form insted of manually scrolling down the pages.

Thanks

Asad

Just do
analogWrite(9,0)
after the pinMode allocation of pin 9.

I have tried but not working

sketch_mar04a.ino (577 Bytes)

Hi,
When you power up what voltage do you have on A1?
At what voltage threshold on A1 do you want the PWM to begin output?

What model Arduino are you using?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Here’s the OP’s sketch:

#include<PWM.h>

float In=A1;
float In1=0;
int Out=9;

float Max=65535.0;
float Duty;
unsigned int Frequency;

void setup() {
Serial.begin(115200);
 InitTimersSafe();
 analogWrite(9,0);
 
}

void loop() {
In1=analogRead(In);
Frequency=map(In1,0,1023,0,50);
//Duty=map(In1,0,1023,0,65535.0);



  Serial.print(In);
  Serial.print("\t");
  Serial.print(In1);
  Serial.print("\t");
  Serial.println(Frequency);
 bool success=SetPinFrequencySafe(Out,Frequency);
 pwmWriteHR(Out, (uint16_t)6553);

 if(In1=0)
{
  SetPinFrequencySafe(Out,0);
}
}

Note that you can't set a zero frequency with that library. You'll have to explicitly use digitalWrite to turn
off the timer, then when going back to a non-zero frequency re-enable the timer.

Also please correct the indentation before posting code, otherwise its very hard to read.

Do not use a float to store a pin number. Only use a float when you intend to store a number that is not an integer in it. Use an int for pin numbers - or better yet, a byte (which uses 1b of ram instead of 2 like an int, or 4 like a float); floating point numbers are also not truly accurate - they are inherently approximate. That said, this is not the cause of the problem.

I try to avoid floating point numbers like the plague, using any trick I can to avoid them. Beyond the inaccuracy I mentioned above, floating point math is all implemented in software (whereas for integers, only division is); each operator you have acting on a float (ie, addition, multiplication, etc) adds to sketch size by a surprising amount. They're also comparatively slow. It's not as hard as you might think at first glance to avoid them.

Reading (analog or digital) on a pin that is not connected to an external voltage will produce random readings - the pin is termed "floating" and will pick up random noise from the environment. You cannot differentiate between a floating pin and a pin with an external voltage applied by reading it - you must ensure via hardware that the pin always has a defined voltage on it (eg, have whatever is generating this voltage always connected), or use some other method to tell it that you've connected something (ex, another pin set INPUT_PULLUP which is grounded when you connect the thing that produces the external voltage).

Hi,
How to structure an if statement;

https://www.arduino.cc/reference/en/language/structure/control-structure/if/

if(In1=0)

should be;

if(In1==0)

Tom.. :slight_smile: