Analogue voltage in, same analogue voltage out.

I want to make a project using an Arduino for my electric bike.

The arduino needs to read a single input voltage from the throttle (approx 0.8v no throttle to 4.3v full throttle) then output the exact voltage to two separate outputs.

For safety, there must be no glitches or power spikes as that could send the bike off without me lol..

Do you think this is possible ? as once that's completed I can start adding features I would like. (even though I know very little Arduino language, but I want to learn)

Thanks for looking :slight_smile:

Yes it can be done.
Look up analogueRead and AnalogueWrite in the reference section.

Read the input voltage, assign it to a variable (maybe call it 'throttleValue')
Write that variable to a PWM enabled output pin.
Write that variable to a second PWM enabled output pin.

Job done.

Write that variable to a PWM enabled output pin.

Sorry, but without some filtering that won't work. PWM is on or it is off, it is not an analogue output.

Rickmk you need an Arduino with a digital to analogue converter, not all of them have one. Read the specifications carefully.

this is the code I have come up with (below) , I think it's correct but the output voltage changes but not true to it's input, probably down to what PerryBebbington has just said. I'll check out if I have any boards with true DA converter and dig some more :slight_smile:

Thank you. (code fixed, sorry)

int throttle_in = A0;    // select the input pin for the throttle voltage input
int PWM_out_pin_one = 3;      // select the pin for the first output
int PWM_out_pin_two = 5;    // select the pin for the second output
int sensorvalue = 0;  // variable to store the value coming from the sensor

void setup() {
 // declare as an OUTPUT:
 pinMode(PWM_out_pin_one, OUTPUT);
 pinMode(PWM_out_pin_two, OUTPUT);
}

void loop() {
 // read the value from the sensor:
 
 sensorvalue = analogRead(throttle_in);
 analogWrite(PWM_out_pin_one, sensorvalue);
 analogWrite(PWM_out_pin_two, sensorvalue);
  
}

Hi Rick,
Please read How to use this forum
Especially item #7 on posting code, then go back and modify your post to put your code in code tags. Thank you.

int throttle_in = A0; // select the input pin for the throttle voltage input

You don't need int, the value will never be negative, use uint or uint16_t.

If you use PWM instead of a D2A converter don't be surprised if you don't get the result you want. At the very least it needs first order filtering; a resistor in series and a capacitor between the resistor (not the PWM pin) and ground. No, I don't know what value, but off the top of my head* maybe 10k** Ohms and 1μF**.

  • Someone clever than me will supply better values!
    ** If you don't have those try whatever you have.

analogRead() is 10-bits (0-1023) and analogWrite is 8-bits (0-255) so you'll have to divide by 4 (or bit-shift right by two bits).

The arduino needs to read a single input voltage from the throttle (approx 0.8v no throttle to 4.3v full throttle) then output the exact voltage to two separate outputs.

Have you tried . . . wire?

DVDdoug:
analogRead() is 10-bits (0-1023) and analogWrite is 8-bits (0-255) so you'll have to divide by 4 (or bit-shift right by two bits).

Absolutely spot on! I divided by 4 and now the output is exact as my input. It's working!! thank you.

One last question if I may, I have added a Nokia LCD that works fine but voltage shown is (example) 3.6705768199. How do I print just "3.67" to the LCD ?

current code.

#include <PCD8544.h>

static PCD8544 lcd;


int throttle_in = A0;    // select the input pin for the throttle voltage input
int PWM_out_pin_one = 9;      // select the pin for the first output
int PWM_out_pin_two = 10;    // select the pin for the second output
int sensorvalue = 0;  // variable to store the value coming from the throttle input

void setup() {
  
 
  lcd.begin(84, 48);
  
  // declare as an OUTPUT:
  pinMode(PWM_out_pin_one, OUTPUT);
  pinMode(PWM_out_pin_two, OUTPUT);
}

void loop() {

// Write a piece of text on the first line...
  lcd.setCursor(0, 0);
  lcd.print("My Ebike v1.0");


  
  // read the value from the sensor:
  sensorvalue = analogRead(throttle_in);
  analogWrite(PWM_out_pin_one, sensorvalue/4);
  analogWrite(PWM_out_pin_two, sensorvalue/4);
  
  lcd.setCursor(0,2);
  lcd.print("Input Voltage");
  
  lcd.setCursor(0,3);
  lcd.print(sensorvalue*(5.0/1023.0), DEC);
  
}

Thanks again :smiley:

lcd.print(sensorvalue*(5.0/1023.0), DEC); Ten decimal places with a float is ambitious.
Try 2 instead of DEC

sp. "1024.0"

rickmk:
I want to make a project using an Arduino for my electric bike.

The arduino needs to read a single input voltage from the throttle (approx 0.8v no throttle to 4.3v full throttle) then output the exact voltage to two separate outputs.

For safety, there must be no glitches or power spikes as that could send the bike off without me lol..

Do you think this is possible ? as once that's completed I can start adding features I would like. (even though I know very little Arduino language, but I want to learn)

Thanks for looking :slight_smile:

Why go through the Arduino for your output?
Simply parallel the signal.

bluejets:
Why go through the Arduino for your output?
Simply parallel the signal.

It's more of a fun project to see if I can get it working and then add things as I go along.

Maybe, If I have a twin motor setup and one of the wheels begin to spin out I can instantly reduce power to that wheel, I am also able to balance different controller/motor combinations.

I can also set limits to the throttle, especially under load with a current sensor.

List is endless but like I say, a bit of fun and learn some Arduino as I go along :slight_smile:

rickmk:
It's more of a fun project to see if I can get it working and then add things as I go along.

Maybe, If I have a twin motor setup and one of the wheels begin to spin out I can instantly reduce power to that wheel, I am also able to balance different controller/motor combinations.

I can also set limits to the throttle, especially under load with a current sensor.

List is endless but like I say, a bit of fun and learn some Arduino as I go along :slight_smile:

So how many drive wheels does this bike have??

Normal approach would be to monitor each wheel speed with say an optical sensor for a given input and regulate the output to avoid the spin.

bluejets:
So how many drive wheels does this bike have??

Normal approach would be to monitor each wheel speed with say an optical sensor for a given input and regulate the output to avoid the spin.

It will be two 1000w motors, two controllers. some people just run with two separate throttles or use a commercial product called cycle analyst that does the limiting.

But as you say, I could probably just connect the one output from the throttle to each motor controller and be done..