Anybody Interested in Arduino Stuff?

Hi Andris,

I use the AEM gauge, comes included with bosch sensor and everything needed.
Has a analog output 0-5V,
Now time to play and learn to connect to an arduino
As most potmeters involved for a stepper or servo are a 0-5 v signal anyway.

Somehow a setpoint ( desired lambda ) and feedback signal ( output sensor set ) is required

Just need to find a good starting point / sketch , reducing the playtime in finding …


https://www.aemelectronics.com/products/dashes_and_gauges/wideband_gauges/x_series_wideband/parts/30-0300?srsltid=AfmBOorxdcAWW9EoJJqv5MaELujo-hZr-zqdrXzzuDnqs8ousKaRgmwB

3 Likes

It looks like you just connect the serial pins and read the serial output from the device. Serial pins are connected tx->rx and rx->tx then that gives you the A/F ratio.

It looks like the rest of it is for the AEMnet stuff. Although they mentioned using a voltmeter to read the readings so if you want to use the raw voltages, then you set it up as the arduino as the voltmeter or logger using the adc. It looks like the grounding is important probably because of floating.

2 Likes

Hi Sean,

The Brown and White wires can be used / are analog 0-5V signal, kinda easy to work with.
the serial is more advanced stuff… :grin:

I am still at beginners level :grin:

2 Likes

You are making it too hard. :slight_smile:

https://docs.arduino.cc/language-reference/en/functions/communication/serial/read/

This is the analog way.
https://docs.arduino.cc/language-reference/en/functions/analog-io/analogRead/

2 Likes

I have some example / set i used in the past to play around, contains some fun info to use.
Instead of pwm, i want to change the output to stepper.


The balance arm has a fan blowing downwards, pivoting on a potmeter ( the feedback signal)
The other potmeter is the setpoint.

I want to replace the feedback with the output O2 sensor set. and the setpoint still use a potmeter to control the setting i want it to be.
The output signal , now pwm, i want to change into stepper.

I have few stepper valves and more steppers.
I think i still have one valve that runs on a pwm output.

have it in my junk corner the last 7 years… :grin:

The arduino program used is based on PID control

fetching the code…

PID control:
unsigned long lastTime;
double Feedback, Output, Setpoint;
double errSum, lastErr;
double ITerm, lastInput;
double kp, ki, kd;
int count_time;
#define PIN_OUTPUT 3 //เอาต์พุต PWM ขับมอเตอร์
void setup()
{
Serial.begin(19200);
}

void loop()
{
count_time++;
if(count_time<=150)Setpoint = 500;
if(count_time>150)Setpoint = 600;
if(count_time>300)count_time = 0;
//อ่านค่าจากตัวต้านทานปรับค่าได้เพื่อบอกถึงตำแหน่งที่ต้องการ
//Setpoint = analogRead(A5);
//อ่านค่าจากเซนเซอร์ตรวจจับตำแหน่ง
Feedback = analogRead(A4);
SetTunings(5.0,0.001,100);// set Kp =2.5
Compute();// Calculate PID Control
Plot();
}

void Compute()
{
/How long since we last calculated/
unsigned long now = millis();
double timeChange = (double)(now - lastTime);
/Compute all the working error variables/
double error = Setpoint - Feedback;
errSum += (error * timeChange);
ITerm = ki * errSum;
if(ITerm<-255)errSum = (ITerm/ki)-(error * timeChange);
if(ITerm>255)errSum = (ITerm/ki)-(error * timeChange);

 double dInput = (error - lastErr);

/Compute PI Output/
Output = kp * error + ITerm + kd * dInput;
if(Output<0)Output = 0;
if(Output>255)Output = 255;
analogWrite(PIN_OUTPUT, Output);

 /*Remember some variables for next time*/

lastErr = error;
lastTime = now;
}

void SetTunings(double Kp, double Ki, double Kd)
{
kp = Kp;
ki = Ki;
kd = Kd;
}

void Plot()
{
// แสดงผลกราฟ
Serial.print("\n");
Serial.print((Setpoint/1023)*100);
Serial.print("\t");
Serial.print((Feedback/1023)*100);
Serial.print("\t");
Serial.print((Output/255)*10);
Serial.print("\t");
Serial.print( 100);
Serial.print("\t");
Serial.print( 10);
Serial.print("\t");
Serial.print( 0);
}

And the code with UseLib
#include <PID_v1.h>// เรียกใช้งานไลเบอรี่PID
#define PIN_OUTPUT 3 //เอาต์พุต PWM ขับมอเตอร์
double Setpoint, Feedback, Output;
int count_time;
//จูนระบบด้วยการปรับค่าเกน Kp และ Ki
double Kp=8, Ki=1, Kd=0.15; // กำหนดค่าเกนทั้งสามตัว

PID myPID(&Feedback, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);//เปิดการใช้งาน PID
void setup()
{
Setpoint = analogRead(A5);//อ่านค่าทั้งต้องการ
Feedback = analogRead(A4);//อ่านค่าจากตัวตรวจจับตำแหน่ง
//turn the PID on
myPID.SetMode(AUTOMATIC);
myPID.SetSampleTime(1);
myPID.SetOutputLimits(0, 255);
Serial.begin(19200);
}
void loop()
{
count_time++;
if(count_time<=150)Setpoint = 500;
if(count_time>150)Setpoint = 600;
if(count_time>300)count_time = 0;

//Setpoint = analogRead(A5);//อ่านค่าจากตัวต้านทานปรับค่าได้เพื่อบอกถึงตำแหน่งที่ต้องการ

Feedback = analogRead(A4);//อ่านค่าจากเซนเซอร์ตรวจจับตำแหน่ง
myPID.Compute(); //สั่งให้ตัวPID เริ่มคำนวณ
analogWrite(PIN_OUTPUT, Output);//ส่งเอาต์พุต PWM ขับมอเตอร์

// แสดงผลกราฟ
Serial.print("\n");
Serial.print((Setpoint/1023)*100);
Serial.print("\t");
Serial.print((Feedback/1023)*100);
Serial.print("\t");
Serial.print((Output/255)*10);
Serial.print("\t");
Serial.print( 100);
Serial.print("\t");
Serial.print( 10);
Serial.print("\t");
Serial.print( 0);
}

Any idea how to change to stepper output ?

1 Like

What kind of stepper motor is it?
This is for 4 wire unipolar and bipolar stepper motors.

https://docs.arduino.cc/learn/electronics/stepper-motors/

Assuming your board is populated. it is using the ULN2003 chip, and this might be what you need. I do not have time to look at your code right now, they called school off because of the cold. it was -9F last night and even now at 11am, it is only 4F. There are more then a few examples using the uln2003, this is just the first one I saw. :slight_smile:

Two other notes.
-there is a scale function, that will convert the arduino analog input to a a value.

-You may need a position sensor or end stops to help determine where the max/min points are for the rotation of the motor. Before you begin, you go all the way to the left, then it hits the endstop which is a pressure sensitive switch, then all the way to the right to the other endstop, to figure out how many steps are in between. There is a way to do it in software using stalls and skipped steps and a distance calculation, but I don’t know how to do it. In 3d printing, they call it virtual end stops. It may or most likely is a function of the motor driver itself, so it may not be an option with the uln2003.

1 Like

Hi Sean,

I had this setup few years back to control a throttle valve ( origin Toyota )

I might try to combine both codes, or use 2 arduino’s to do the job :smile:

This second one is a toyota throttle valve by PWM

Arduino code, i am still a noob that just is able to copy paste others or existing code :grin:

1 Like

you just need one arduino. you piece the snippets together and you have to change the pin assignments to what you are using. :slight_smile:

You could also use a raspberry pi pico. Those support micropython if you are more familiar with python. My current favorite language is Go. The snippets I posted were in C/C++ though.

I still want to poke through your code and go back and look at the wiring.

2 Likes

The board isn’t going to work for a bi-polar stepper motor because it appears the motor output only has one wire for the fan. And I don’t think it works for unipolar steppers either because I don’t see how it reverses the current. the uln2003 chip will work but it requires a different circuit. Which you can unsolder the chip, and use that.

The TMC2209 bi-polar stepper drivers might be easier. They are used for 3d printers. They can check the stall current, and thus don’t need the endstop/physical switches to detect when they are wide open or fully closed. It simplifies the wiring. They are like 5 bucks for a tiny board.
https://www.aliexpress.us/item/3256808088283057.html?

The arduino library exists:
https://docs.arduino.cc/libraries/tmc2209/

1 Like

Hi Sean,

Pictures say more than words…
I have more gizmo’s around and want to do…
But…
So little knowledge on how to do :grin:
I will build the first set with PWM output for the DBW valve.

It looks as a good starting point for use with gasifiers/car engines.
The setpoint can be adjusted from the driver into desired level, the arduino will command the valve into correct desired value according the O2 sensor output ( feedback)





5 Likes

Throttle bodies typically use servo motors. Servo motors have a position sensor circuit built into them so you can send them a pwm signal, and they move into position. A stepper motor does not have that. So you can think of a servo motor as a stepper motor with a built-in arduino to take a pwm signal to move it into position.

If you just have a stepper motor, you have to figure out how you are going to determine the position. A servo motor does this with usually an optical, or magnetic position sensor or a rotary encoder.

The easiest way with a stepper is to add switches at start and stop points. But you can also do it by measuring the stall current when it hits something the motor stalls. You might be able to get away with a spring to start at zero. Most stepper are 1.8 degrees per step, or 50 steps for opening a 90 degree butterfly.

3 Likes

A lot of 3d printers use steppers without rotatory encoders and really without any direct position feedback. They do have end stops though which are just cheap little micro switches.

At start-up the printer runs the steppers in one direction until the “head” hits the end stops. From there the controller is just counting steps back and forth and assuming the stepper motor is doing the dance that the controller is feeding it.

A missed step is a real problem. Sometimes the printer will rehome periodically get a fresh calibration. Too bad about any missed steps that happened prior. The other “fix” is to use strong steppers and drive them hard enough that the motors don’t miss.

For a throttle controller you don’t really need positional feedback, just mix feedback. Mix needs more 02? Open the valve. Mix is running lean? close the valve a bit.

4 Likes

Just to “stir the waters” some more,
There’s this Australian guy named Josh Stewart who has been developing engine ECU’s based on Arduino for many years.
Just google “speeduino” or click:

and bring a fresh cup of coffee.
Or if you are a github person:

Pete Stanaitis

3 Likes

Who remembers the early days of PCs? The floppy drives (and some early hard drives) used steppers with hard mechanical stops. Turn on the PC, BzzzzzzzZZZT. 50 tracks? Then go 75 steps in the direction of track 0, and you’ve got to be there, right? You could hear the buzz get louder when the disk drive stepper hit the mechanical stop. Elegant, no. Feedback, no. Cheap and robust, yup.

3 Likes

They either use endstop switches, or they use virtual endstops using the TMC drivers or newer series of stepper drivers as those give feedback.

You are right you don’t necessarily need it unless something screws up like skipped steps or a bad sensor and it tries to move past open or closed or gets stuck then it can potentially burn up the motor and motor driver.

I think it was 40 tracks, and the disk spun at a fixed rpm, the stepper moved the head from track to track not in the circular motion. The buzz got worse when the head got out of alignment. My guess is it measured stall current with an external sensor. so it hit, then it picked up the measurement, and stopped. It may of had an external mechanical switch. I really don’t know.

2 Likes

I noticed you had an l298 stepper driver.
This is a pretty nice tutorial to use that and work through the wiring and pin settings.

in your code you start adding the actual functionality in or near analogwrite() functions. You need to set up the libraries, and you probably want a global variable to attempt to keep track of position.

edit: You can add endstops switches on a wheel or lever on the shaft with a pin sticking out to hit the switch at both ends. Then you wire the switches to other gpio pins (add a resistor in there to protect the pin.) Then check the gpio right after you move so you can move 1 step in the opposite direction you just moved if it hit the switch.

These are what the 3d printer limit switches look like and they have the resistor on them.
3D Printer Part End Stop Limit Switch,5 Pcs Micro Mechanical Switch 3 Pin Compatible with CNC RAMPS 1.4 RepRap 3D Printer CR-10 10S,S4,S5,Ender 3/Ender 3 Pro/Ender 3 V2 by GUBCUB: Amazon.com: Industrial & Scientific?

but all are you doing is checking to see if the circuit is open or closed (depending on if it is a normally open or normally closed switch) so you can use any switch or make your own even a button switch will work. or a level with a wire on it to complete a circuit. You might have to do a pause 20ms or whatever to wait for the motor to move first.

3 Likes

Hi Sean,
I do have a lot of different stuff around as i am not so much limited in trying out things.
Based on your info, i have been studying the different roads i have ahead and i can tell what kind of different types are used /in use or what i have present.

The throttle body’s i have are either DBW or stepper., where as the DBW is just a dc motor working against a loaded spring, acting according the power /duty cycle of the PWM input. take away the pwm powewr and the valve returns to its zero position.
Stepper, i have unipolar on the throttle body’s but i use them either in halve coil bipolar or double coil bipolar.
Servo’s act or are build a bit different, the input signal is a specific pwm signal that either commands
the servo dc motor driver to go clockwise or counter clockwise. a certain , servo specific pwm dutycycle ms wil hold the servo at standstill an higher or lower dutycycle will change the direction.

as said before, many things wanting to do, but so many things still i need to learn.

Many thanks for the input so far

4 Likes

Because you have so many things, I can’t quite figure out what you have and how to help you. If you are trying to build the complete ECU, then the ideal O2 reading varies with RPM which adds another layer of complexity.

A lot of this stuff, it is easiest to work through the small code snippets to figure out how they work along with the wiring, then you assemble them together in your final program. To be honest, sometimes, you just need to write it out into a flow chart. Pseudocode, which is just writing out basically an outline of the logical steps can also be very helpful as well.
Once you have it written out, then it is just writing out the code, which can be picky, but it syntax not logic.

4 Likes

Hi Sean.
That exact method has worked out well for me for the last 40 years or so.

4 Likes