.ino for PCB+MD13S+Danfoss valve

I’ve tried to learn to read the code, but I just do not understand it. Sorry.
I’m still waiting for someone to add the steering wheel encoder function to PCBv2.ino. I’d like to use it on my own devices instead of the coffeetrac button.ino. But that’s another story. :grin:

@Kaupoi well i’m in the process of rewriting a lot. Going to make that ino about 10% of the size it is now. It’s too frickin complicated for a piece of software that turns a motor left and right.

1 Like

Maybe it is enough to copy the 4 or 5 lines in coffeetrac ino, where the word encapin appears, and put them in pcbv2 ino about the same places. The D2 pin doesn´t seem to be used in pcbv2 ino.

And also this part from setup zone.

#define SWEncoder 0 // Steering Wheel ENCODER Installed
#define pulseCountMax 3 // Switch off Autosteer after X Pulses from Steering wheel encoder

There are a few places. Can you just use the coffeetrac ino?

Yes I can. But doesn’t pcbv2.ino have better roll filtering?

File Autosteer_switch-master.zip unzip and open I arduino.

In file “PID.ino”

write on botom file, or copy-paste:

#if (Output_Driver == 4) //PWM 50% if pwmDrive ~ 0 , PWM 0% if pwmDrive -255 …
void motorDrive(void)
{// Used with Danffos valve 50% pwm off valve

pwmDanffos = (pwmDrive/2)+128;

if(pwmDanffos > 195) pwmDanffos = 195; // max 0,75 U , PWM 75%
if (pwmDanffos < 1 ) pwmDanffos = 48; // min 0,25 U ,PWM 25%

analogWrite(PWM1_PIN, pwmDanffos);



In file “Autosteer swetch” on top file write “4” :

#define Output_Driver 4

same file, need add “pwmDanffos = 0 “ :

//pwm variables
int pwmDrive = 0, drive = 0, pwmDisplay = 0 ,pwmDanffos = 0;
float pValue = 0, iValue = 0, dValue = 0;

Need test and PWM duty cycle should have range between 25% to 75%.

looks fine and I think I understand the settings. Except where the numbers -255 and 255 come from?
And please spell the company name Danfoss correct :slight_smile:


PID calculation:

pValue = steerSettings.Kp * steerAngleError *steerSettings.Ko;

/* //Derivative
dError = steerAngleError - lastLastError;
dValue = Kd * (dError) * Ko;

//save history of errors
lastLastError = lastError;
lastError = steerAngleError;

drive = pValue;// + dValue;
pwmDrive = (constrain(drive, -255, 255));

//add throttle factor so no delay from motor resistance.
if (pwmDrive < 0 ) pwmDrive -= steerSettings.minPWMValue;
else if (pwmDrive > 0 ) pwmDrive += steerSettings.minPWMValue;

if (pwmDrive > 255) pwmDrive = 255;
if (pwmDrive < -255) pwmDrive = -255;

8 byte PWM resolution on arduino nano(255) , for driver direction DIR pin, and PWM ,DIR pin LOW left side -255 max write to PWM 255 , right side DIR pin HIGH 255 max write to PWM 255 for max duty cycle.

For Danfoss valve not need DIR pin for change direction.

If max left from PID calculation “pwmDrive” = -255

from code pwmDanffos = (pwmDrive/2)+128;

(-255/2) + 128 = ~ 0 min duty cycle

if max right PID calculation “pwmDrive” = 255

(255/2) + 128 = ~ 255 max PWM duty cycle

If pwmDrive from PID calculation 0 , no stering error PWM 128 , valve close

sorry about my english

1 Like

PWM to Voltage Converter Module 0%-100% to 0-10V

I don’t understand the code at all, but thanks anyway :smiley:
I wouldn’t recommend using switch.ino with hydraulic steering. Maybe some day, someone will put that code in the PCBv2.ino :wink:

For the Danfoss valve, the mentioned converter is not needed and can only handle 100 mA and danfoss uses up to 700 mA. So we must use a motorcontroller as “pwm converter”.
On the signal pin (U s) It needs between 25 and 75 % of supply voltage, so in our case we would probably have 12 v on supply pin U dc), which demands a feed of something between 3v and 9v to the input/signal pin on the danfoss head to control the valve.

On yours link says:
Signal current at rated voltage 0.25 mA to 0.70 mA

700mA are 7000 x > 0,70mA

You are absolutely right, I also did write 0,70 mA in my first post and changed it to 0,7 A today.
My fault was to read page 8 too quick saw 0,3 A in the scheme and then went back here.
I rechange the first post to mA :unamused:

Coffeetrac arduino program for Danfoss valve 0,75 to 0,25 PWM for IBT-2 driver motor, and steering Wweel ENCODER :

1 Like

Its been there since the summer.

So I had to do the test, used the coffeetrac button ino i downloaded last summer, and edited it to nr. 4 Danfoss.
And Yes it works right out of the box(no other change to ino than 4 instead of 2) And I have around 6 V straight ahead.
Although the used pins changes: instead of D9 I had to use D4 as PWM2 for my IBT_2. I use pcbv1
D5 is still used to activate/enable L_en and R_en on IBT_2

BUT I still can´t find out where and how the offset is done, so I am still at the same lack of understanding as i write in posts around 15 and 17 above

1 Like


In “PID.ino”

code : " pwmOut = pwmDrive+ 128; // add Center Pos. "

change 128 to 127 or 126, or 129 , 130 … output change to 6.1 or 6.2 etc… or 5.9V , 5,8V

In PID.ino existing 2 line of code : pwmOut = pwmDrive+ 128;

change both and compile.

  1. Valve use DC voltage for control not PWM.

  2. For offset by DC DC voltage regulator 5-6A ,connect power supply from voltage regulator to valve. Danfoss valve current consumption at rated voltage 12V are 0.33A. On voltage regulator thru changing voltage you tune offset on front tires .

  3. by PWM to DC voltage converter (post above) , connect with voltage regulator above(power supply) , or better separate voltage regulator but smaller , constant voltage, not drift. Connect from arduino PWM and GND to PWM to DC converter.

  4. No need change nothing in software for tune offset in driving tires , hardware setting offset with screwdriver.

  5. Turn off auto steering from signal from clutch , brake ……………………….

On this video coffeetrac use analog voltage for driving valve.


Now I see the connection: PID.ino opens automatically together with autosteer_button.ino and you can find it by pressing the “arrow down sign” at top right side of arduino program screen.
So when compiling all the files mentioned in the top line are “put together” before uploading to nano.
Thank you for your patience grabik :slight_smile:
I like the brake or clutch idea, as a “footswitch” mentioned in line 5 above.