Just testing at the moment but I have a standard PC PWM fan, runs from a minimum of approx 30% to 100%
I have connected the PWM wire to pin 9 (&10 &3) of a UNO, then a 12v power supply to the fan positive and the negative to the ground on the UNO as well as the fan. So far so good, fan sppeds up to maximum then down to min and stays there.
SO then I tried both of the PWM options inside sketch at 3 different maximum settings, the default 255, 79 (this will become clear later) and 999. once it is all configured a push on the test in motors gives a small increase and a log recording of setting the device to 11, when it is set to 79 nothing happens at all.
Attached to iracing the fan peaks at about 40% max, I tried setting the speed and % options for control. No difference. After hours of reading I found a different article for Assetto Corsa which uses a different program. To test I loaded that, as part of it's startup it ramps the speed up from minimum in small increments up to maximum showing a duty cycle of lowest 0 and highest 79. this test works perfectly on my fan, spins gradually up to full speed (I changed the ramp up time to .25 second rather than 50ms to hear the speed differences.) then runs at full speed for 3 secodns then drops to minimum.
So I know the UNO can control the fan properly but cannot make it work with simvibe. This is the AC code that does work.
Anybody able to help?
Code:
// Arduino UNO sketch
// Insert Coin
// Version 1.0: April 1, 2017
// Version 1.1: May 26, 2018
//
// Thanks to afremont for his PWM logic,
// see https://forum.arduino.cc/index.php?topic=155089.45
//
// Thanks to mongo56 for his idea ACFan and source code,
// see https://www.reddit.com/r/oculus/comments/2b8d3m/assetto_corsa_fan_control/
//
// Version 1.1: higher minimum speed of the fans while driving slowly
// Maximum speed of the fans now set at 250 km/h
//
String incomingValue;
int myval;
unsigned int x;
const int PWMPin = 3;
void setup() {
myval = 0;
Serial.begin(9600);
// generate 25kHz PWM pulse rate on Pin 3
pinMode(PWMPin, OUTPUT); // OCR2B sets duty cycle
// Set up Fast PWM on Pin 3
TCCR2A = 0x23; // COM2B1, WGM21, WGM20
// Set prescaler
TCCR2B = 0x0A; // WGM21, Prescaler = /8
// Set TOP and initialize duty cycle to zero(0)
OCR2A = 79; // TOP DO NOT CHANGE, SETS PWM PULSE RATE
// start-up sequence: let the user know we are working OK
// start fan at lowest speed
OCR2B = 0; // duty cycle for Pin 3 (0-79) generates 1 500nS pulse even when 0 🙁
// wait for the fan to power down
delay(1000);
// ramp up fan speed by increasing duty cycle every 50mS, takes 4 seconds
for(x = 0; x < 80; x++) {
OCR2B = x; // set duty cycle
delay(50);
}
// wait a bit
delay(3000);
// set fan to lowest speed
OCR2B = 40; // duty cycle for Pin 3 (0-79) generates 1 500nS pulse even when 0 🙁
}
void loop() {
// waiting for data coming from ACFan.exe
// which retrieves the speed from Assetto Corsa,
// converts it to a string and sends it to the Arduino
if(Serial.available() > 0)
{
char ch = Serial.read();
// check for new line and add the char if it isn't
if (ch != 10){
incomingValue += ch;
}
else
{
// Serial.println(incomingString);
int val = incomingValue.toInt();
// Text copied from ACFan.cpp:
// ESC value between 30 and 180 (30 = initial startup)
// adjust this to fit your ESC/motor and liking
// 61 was the value my motor started running
// 350 was my max speed in Assetto Corsa (in kmh)
// 50 was an arbitary multiplier to normalise the output values between ~61-100
// also added a bit of variance with steerAngle so that when you turn in you get a little less "wind"
if (val > -1 && val < 181)
{
// Version 1.1, another approach: about 30% low speed boost
if (val < 63)
{
myval = 0; // fans to 0% when the car isn't moving
}
else {
myval = (val - 35) * 1.25;
// To get a variance between 0 and 79 instead of 61 and 100.
// 1.25 = max speed around 250 km/h, decrease this value for faster cars.
// Higher minimum speed of the fans.
}
// check if out of bounds
if (myval < 0) {
myval = 0;
}
if (myval > 79) {
myval = 79;
}
// set the fan speed (duty cycle)
OCR2B = myval;
}
// clear the input string that came from ACFan
incomingValue = "";
}
}
}
Hi !
First of all a small question, are you using the PWM fan dedicated entry ? The other PWM entry can work with some fans, but is less reliable for this application.
This specific generates a standard "fan compliant" 25khz PWM signal (totally different from arduino default one, a bit like the sketch you linked) but can only be used on pins 9 and 10 on UNO as shown in setup tool comments, you won't have any luck with any other pins.
I've already seen fans simply slowing down or stopping when passed a specific value, the key is to understand what is the working range of your fan, here are some detailed steps :
- Upload the sketch with the standard 255 max PWM
- Make sure to push to 100% the top right general volume sliders in simhub :
- Add a static wind effect so you can output a permanent signal (uncheck run only when in race), and set everything to 100%
- At this point you will get a 100% permanent signal visible in the output :
- If somehow the signal is not 100%, look if some advanced balancing gains have not been changed, and make everything 100% :
- At this point you will have a 100% signal going to the arduino
- Now play with the general gain slider, it will give you a direct control to the output (50% will give 128 on the arduino for instance) the idea is to check the effective working range. If you see a specific value where the fan stops let's say 50% general volume in simhub, it means the fan can work only at 50% max output so the max value must be set to 127 (255 * 50 /100). Move slowly as fans have some latencies.
When properly calibrated now you can disable the static wind and go back to the classic speed effect.
Let me know if it helped,
Nicolas
I’ve tried both and identical results but I’ll move to that one. And some great troubleshooting tips! Thank you. I’ll try this tomorrow after work.
I'm hitting the same issue. Running a similar sketch to the one @trevoraustin linked is able to control the fans (I alternated between full and stop every 5 seconds in a loop, worked just fine), but not SimHub. I was able to get a different set of DC fans working just fine with a motorshield, but not the PWM fan using pins 9/10 and the PWM Fans Sketch. I've got Noctua PWM fans which are able to fully stop. I walked through the above steps you provided. Interestingly enough mashing "Test Now" at a zero or very low output on the Motor Output page is able to stop the fan, but it's not getting any signal from the effects page. Looking forward to getting this resolved 🙂
That's really strange indeed,
I've just checked again with the two PWM fans I have under hand cooler master "MasterFan 120AB fan" (stop capable) and a no-name 7000 RPM (non stop capable) mining fan, aside of a few lost fingers during the operation, they both worked perfectly, the only trick is related to the fan minimum signal, some fans will start to spin only when reaching a minimum value (often 30%) , some will start straight to 30% and stick to this speed until you reach the 30% value then start to go faster.
In all cases with fans it's interesting to make sure the volume is at it's maximum.
I've also checked a few times in the past the frequencies and signal shape using an oscilloscope to make sure it was perfectly within the "spec"
Let me know.
A short video of the test :
As you can see it speed starts to change a bit late (~16% for this particular fan) then progressively raise, I had to stop at 50% to protect my fingers and my desk 😀 but this fan goes much faster when reaching 100%.
Well that's a bit embarrassing, I knew half way through setting up the test what I had done wrong!!!
The profile default fan speed at the top of this page attached is set to 50%, so when I set it to 100% in the wind motor it was still 100% of 50% not overriding the profile default but a multiplier of it. As soon as I put that to 100% of course it worked perfectly 🙂 Doh!
Thanks for your excellent and very quick response, and damn I wasted 3-4 hours messing about with various settings on this yesterday!!!