PWM

Hi all,

I am designing a circuit which will read analog values from an IR and map the value to PWM out.
The problem is when Analog read is at its mid point which is arount 511. The arduino should give PWM out 0 and 255 for an instance then back to the normal mappng.

mapping is working . i am using if condition for this instance pulse but nothing happening.

Please help .

Asad

post your code. select the "</>" top left

int a=A0;
int out= 2;
void setup() 
{
  
  Serial.begin(9600);
  pinMode(out,OUTPUT);

}

void loop() 

{

  
  int x= analogRead(A0);
  int s=map(x,0,1023,255,0);
  
  Serial.print(x);
  Serial.print("\t");
  Serial.println(s);
  analogWrite(out,s);
  
  if(x<=515 && x>=505)
  {
    analogWrite(out,255);
    analogWrite(out,0);
    return;
  }
  delay(250);



}

It sounds like line 22 should really be after line 29 - but I'm not sure that I understand what you want to happen.

analogWrite(out, 255) followed by analogWrite(out,0) will happen so quickly as not to be noticeable

It is much easier to provide useful advice if you describe the project you are trying to create.

...R

i am controlling DC motor circuit for cloth rolling machine IR sensors are installed on the rolling machine which keeps the cloth align from both sides
Reading the IR sensors data on arduino then sending control signal to the motor control board.
Motor control signal is -12V moving motor to left and +12V to move to the right and 0V at center.
When motor comes to center due to inertial load motor dont stop at center it moves to left or right ie the opposite side from which it is coming ..

I am writing code That when motor comes to center it arduino gives both signals ie -12 and +12 V so motor applies break

for -12 and +12 i am using amplifier IC whose input is 0 to 5V from arduino and output is -12 to +12 V respectively . output is 0 at 2.5 V from arduino.

if you're using an h-bridge, there is only one PWM output and bit(s) to control the direction and braking.

can you post a circuit (description)?

asad9266:
I am writing code That when motor comes to center it arduino gives both signals ie -12 and +12 V so motor applies break

The way you are doing it seems strange, but if it works ...

for -12 and +12 i am using amplifier IC whose input is 0 to 5V from arduino and output is -12 to +12 V respectively . output is 0 at 2.5 V from arduino.

The PWM output from an Arduino is not a variable voltage. It is an ON-OFF signal of 0v or 5v with a varying duty cycle. You need external components to convert that to a varying voltage.

Note that analogWrite(pin, 0) is the same as digitalWrite(pin, LOW) analogWrite(pin,255) is the same as digitalWrite(pin, HIGH)

Coming back to your original question, can you describe in more detail the sequence that should happen (with timing) when the system approaches the centre? It sounds like a case for using PID control to avoid an overshoot.

...R

asad9266:
Hi all,

I am designing a circuit which will read analog values from an IR and map the value to PWM out.
The problem is when Analog read is at its mid point which is arount 511. The arduino should give PWM out 0 and 255 for an instance then back to the normal mappng.

mapping is working . i am using if condition for this instance pulse but nothing happening.

Please help .

Asad

Hey,
Don't use the the "if else" statement for mapping of values. Use the map() function. You can see it here.

..Arnav

ArnavPawarAA:
Hey,
Don't use the the "if else" statement for mapping of values. Use the map() function. You can see it here.

..Arnav

Don't you even bother looking at the code before offering your useless advice? The OP is using map() for mapping and the if statement is for selection which is its intended purpose.

Steve

Robin2:
The way you are doing it seems strange, but if it works ...
The PWM output from an Arduino is not a variable voltage. It is an ON-OFF signal of 0v or 5v with a varying duty cycle. You need external components to convert that to a varying voltage.

Note that analogWrite(pin, 0) is the same as digitalWrite(pin, LOW) analogWrite(pin,255) is the same as digitalWrite(pin, HIGH)

Coming back to your original question, can you describe in more detail the sequence that should happen (with timing) when the system approaches the centre? It sounds like a case for using PID control to avoid an overshoot.

...R

yes I know about the PWM output of Arduino. i am using an amplifier IC to convert PWM to analog -12 to +12 V. The motor control circuit is built on 2 thyristors half wave assembly. which is already attached to the machine. i am designing only the input signal for that circuit. it moves the motor to left when -12 V signal is applied and moves right when +12V is applied. I am trying to do Is Breaking off the DC motor. which will achieve by turning both thyristors ON for a short moment.

PS: I have posted working of the system before up here.

int out= 2; Err.

asad9266:
yes I know about the PWM output of Arduino. .....

You have not responded to my request for more detail about the sequence of events you want the Arduino to produce.

...R

int out = 2;
  analogWrite(out, s);

What Arduino are you using? Pin 2 on an Arduino UNO is not a PWM pin.

johnwasser:

int out = 2;

analogWrite(out, s);



What Arduino are you using? Pin 2 on an Arduino UNO is not a PWM pin.

Psst, reply #10, six hours ago.

TheMemberFormerlyKnownAsAWOL:
Psst, reply #10, six hours ago.

I am using Arduinno Mega

asad9266:
I am using Arduinno Mega

...thirty hours later.

Robin2:
You have not responded to my request for more detail about the sequence of events you want the Arduino to produce.

...R

Arduino Read values from IR sensor. IR is installed in a rolling machine which detects the position of Cloth running on rolling machine to keep it align.

. for 1023 it will give 0 of PWM and at 0 it will give 255.

then that value go to amplifier which will convert this signal to -12V to +12 V.

That signal will go to the motor control card.

0V to -12 V the motor moves to left AND +V to +12 V motor moves to Right. the higher the value, higher the speed of the motor.

Motor is used to align the assemble.

Motor and that assembly have some inertial load due to which we require to apply breaks to the motor which will achieve by applying -12V and +12V signal ie 0PWM and 255PWM from arduino for a very short instance and then keep giving value according to the analog input.

Breaking is achieve at midpoint analog value 511, PWM output at half ie around 125. but for a short moment when it comes to mid it should give 0 and 255.

I am not being able to achieve the breaking procedure that is mentioned above

for 1023 it will give 0 of PWM and at 0 it will give 255.

PWMvalue = 255 - (analogRead (inPin) / 4);

  if(x<=515 && x>=505)
  {
    analogWrite(out,255);
    analogWrite(out,0);
    return;
  }
  delay(250);

What you sketch is doing is, whenever the analogRead() returns a value from 505 to 515 (inclusive) it sets the PWM output to 128-ish for a few microseconds, then 255 for a few microseconds, then 0 for a few microseconds and then repeats very quickly. How did you want it to act differently? Did you want the massive swing to NOT be repeated? Did you want the 128-ish value to not happen when the 0/255 swing happened? WHAT EXACTLY DO YOU WANT?

asad9266:
Breaking is achieve at midpoint analog value 511, PWM output at half ie around 125. but for a short moment when it comes to mid it should give 0 and 255.

How long is this short moment.

For how long must the value stay at 0 and for how long at 255 ?

...R