PWM with an arduino
Posted: Saturday 22 August 2020 6:38
Hello guys ,
I am trying to integrate an arduino nano-based assembly into domoticz which allows me to manage the speed rotation of a fan (PWM) from 0 to 100.
The arduino and the Raspberry pi are connected via USB and I already have a python script which allows me to manage it from the terminal.
For the moment, in domoticz I have a virtual switch (dimmer).
I need help for a script that varies the speed of my fan according to my virtual ...
The script I am using on the RPI is posted below.
here is the one on the arduino:
I am trying to integrate an arduino nano-based assembly into domoticz which allows me to manage the speed rotation of a fan (PWM) from 0 to 100.
The arduino and the Raspberry pi are connected via USB and I already have a python script which allows me to manage it from the terminal.
For the moment, in domoticz I have a virtual switch (dimmer).
I need help for a script that varies the speed of my fan according to my virtual ...
The script I am using on the RPI is posted below.
Code: Select all
[Raspberry pi fan control and monitoring with bash](https://www.domoticz.com/wiki/Raspberry_pi_fan_control_and_monitoring_with_bash)
Code: Select all
const byte OC1A_PIN = 9;
const byte OC1B_PIN = 10;
const word PWM_FREQ_HZ = 25000; //Adjust this value to adjust the frequency
const word TCNT1_TOP = 16000000/(2*PWM_FREQ_HZ);
byte in = 0;
void setup() {
pinMode(OC1A_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Demarage du PWM"); // so I can keep track of what is loaded
// Clear Timer1 control and count registers
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
// Set Timer1 configuration
// COM1A(1:0) = 0b10 (Output A clear rising/set falling)
// COM1B(1:0) = 0b00 (Output B normal operation)
// WGM(13:10) = 0b1010 (Phase correct PWM)
// ICNC1 = 0b0 (Input capture noise canceler disabled)
// ICES1 = 0b0 (Input capture edge select disabled)
// CS(12:10) = 0b001 (Input clock select = clock/1)
TCCR1A |= (1 << COM1A1) | (1 << WGM11);
TCCR1B |= (1 << WGM13) | (1 << CS10);
ICR1 = TCNT1_TOP;
}
void loop() {
if (Serial.available()) { // Check if there's data
char buf[10];
int in = Serial.readBytesUntil('\n', buf, sizeof buf - 1);
buf[in]=0;
in = atoi(buf);
if (in > 100)
in = 100;
Serial.println(in);// Pass the value of "in" to the pin}
setPwmDuty(in); //Change this value 0-100 to adjust duty cycle
}
}
void setPwmDuty(byte duty) {
OCR1A = (word) (duty*TCNT1_TOP)/100;
}