Arduino drives 12v solenoid with pwm

hi
i want to drive 12v solenoid with arduino
but i must drop frequency to 10-50 hz

the connection and the driver its all write but the pwm fucion its the problem
can anyone help me?

Different processors have different choices of timers so the answer will be different for UNO, MEGA, Leonardo... Which Arduino are you using?

i am using arduino uno but at the future i want to use arduino mega
i dont know if they have different in programming

bill_lask:
i am using arduino uno but at the future i want to use arduino mega
i dont know if they have different in programming

They are not different in programming other than that the SPI pins and some more things are positioned on other pins I.e.

firstly i want to work with uno and when all work ok with mega

So what's the problem?
Use blink without delay and create whatever frequency you want.

unsigned long currentMicros();
unsigned long previousMicros();
unsigned long duration = 50000; // 50mS, 1/2 period of 10 Hz

byte pin2PWM = 2; // PORTD, bit 2

void setup(){
pinMode (pin2PWM,  OUTPUT);
previousMicros = micros(); 
}

void loop(){
currentMicros = micros();
if ( (currentMicros - previousMicros) >=duration){
previousMicros = previousMicros + duration; // set up for next level change
PIND = PIND | 0b00000100; // toggle output bit by writing to input register
}
// do other stuff while waiting for next time change

}

I would read in the ATmega328P datasheet about implementing PWM on Timer/Counter2 (the 8-bit timer). That will give you PWM on pins 3 and/or 11. When you switch to the MEGA you will probably have to switch timers and/or pins. Timer/Counter2 on the MEGA does PWM on pins 9 and/or 10

Seems like overkill for 10 Hz to 50 Hz. Will the hardware PWM even go that slow?
50 Hz is system clock divided by 320,000.
10 Hz is system clock divided by 1,600,000.

i want to do with interupt because in main loop i do a lot of things and maybe it didint works with millis :confused:
can you help me with interrupt timer?

i find this http://roboexperiments.com/beginners-guide-avr-pwm-frequency-duty-cycle/

and i make this:

#include <avr/io.h>



int main(void){

DDRD = 0xff;

TCCR1A = 0;

TCCR1B = 0;

TCCR1A |= ( 1<<COM1B1 | //non-inverted mode

1<<COM1B0 |

1<<WGM11 ); //fast PWM (14) ,



TCCR1B |= ( 1<<WGM12 |

1<<WGM13 |

1<<CS10); //prescale 1

int top=(16000000/ (1*frequency))-1
int dt=(duty/100)*top

ICR1 = top;



while(1){

OCR1B = ICR1 - dt;

}

}

but how can write this in fuction with timer to give frequency and duty and make the pwm?