How to add a throttle to a Carrera Vengeance E Spec

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
ok so i have deviated a little from the original pin design we set out but i hope it still makes sense, I attempted to work from the original post in terms of wiring (just for my own sanity so I'm working from something proven). Also I have commented out the digital interrupt while i experiment with throttle. I'll do some more testing on the weekend.

My Pins are as follows (attempting to stay closely with the original pin layout in the original post):

PAS Cadence Input Pin - D2 (Digital)
PAS Torque Input Pin - A3 (Analog)
Throttle Input Pin - A0 (Analog)
Controller Cadence Output Pin - D13 (Digital)
Controller Torque Output Pin - A5 (Analog)

Code:
// Experimenting
int pulseoff = 600;
int pulseon = 0;
int val = 0;
int throttleperc = 0;
int torqueperc = 0;
int torqueval = 0;
bool pulse = false;
bool throttle = false;
int pedalin = 2;
unsigned long startMillis;
unsigned long currentMillis;
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(A3, INPUT); //PASTorquePin
  pinMode(A5, OUTPUT); //ControllerTorquePin
  pinMode(pedalin, INPUT);
  digitalWrite(LED_BUILTIN, LOW);
  //attachInterrupt(digitalPinToInterrupt(2), pedalon, RISING);
  startMillis = millis();
  Serial.begin(9600);
}
void loop() {
  currentMillis = millis();
  val = analogRead(A0); // Read the value from the throttle pin A0
  throttleperc = ((float)(val - 290) / (float)577) * 100; // calculate percentage increase decrease in throttle 577 is the range from min-max 290 minimum
  torqueperc = 164 * ((float)(val - 290) / (float)577); // calculate the torque increase / decrease value from max 164 range
  torqueval = 76 + torqueperc; // 76 minimum using range 164 it will never exceed 240 which is 4.3v
  pulseoff = (600 - (val / 1.7)) + 120;
  pulseon = int(pulseoff / 3);
  if (pulse == false) {
    if (pulseoff < 550) {
      if (throttle == false) { //removes the halfsecond possible delay on first start up.
        digitalWrite(LED_BUILTIN, HIGH);
        throttle = true;
        pulse = true;
      } else {
        analogWrite(A5, torqueval); // This value will always be in a range from 1.5v (76) to 4.3v (240) outside of the pulsing IF statement
        if (currentMillis > (startMillis + pulseoff)) { //send pulse
          digitalWrite(LED_BUILTIN, HIGH);
          pulse = true;
          startMillis = millis();
          throttle = true;
        }
      }
    } else {
      throttle = false;
    }
  } else {
    if (currentMillis > (startMillis + pulseon)) {
      pulse = false;
      digitalWrite(LED_BUILTIN, LOW); // pulse off signal to controller
      analogWrite(5, torqueval); // will always be minimum 76
    }
  }
}

void pedalon()    //Called when pedal is moved
{
  if (throttle == false) { //Only passes signal if throttle is not being used, otherwise ignored.
    if (digitalRead(2) == HIGH) {
      digitalWrite(LED_BUILTIN, HIGH);
      analogWrite(A5, analogRead(A3)); //Attempt to pass what ever value comes from the Torque sensor straight to the controller
    } else {
      digitalWrite(LED_BUILTIN, LOW);
      analogWrite(A5, analogRead(A3)); //Not pedalling anymore so again just pass the torque value straight through to the controller
    }
    Serial.println("pedal round..");
  }
}
 
Last edited:

Woosh

Trade Member
May 19, 2012
20,225
16,819
Southend on Sea
wooshbikes.co.uk
here is my suggested changes to your code. No warranty given whatsoever, use it at your own risks etc..
Code:
// Experimenting
// PAS Cadence Input Pin - D2 (Digital)
// PAS Torque Input Pin - A3 (Analog)
// Throttle Input Pin - A0 (Analog)
// Controller Cadence Output Pin - D13 (Digital)
// Controller Torque Output Pin - A5 (Analog)

int pulseoff = 100; // milliseconds when synthesised cadence is low
int pulseon = 66;   // milliseconds when synthesised cadence is high 6Hz
int val = 0;
int torqueval = 0;
bool pulse = false;
bool throttle = false;
int pedalin = 2;
unsigned long startMillis;
unsigned long currentMillis;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(A3, INPUT); //PASTorquePin
  pinMode(A5, OUTPUT); //ControllerTorquePin
  pinMode(pedalin, INPUT);
  digitalWrite(LED_BUILTIN, LOW);
  startMillis = millis();
  Serial.begin(9600);
}

void loop() {
    if (millis()>currentMillis) // run once every millisecond
    {
        currentMillis = millis();
        val = analogRead(A0); // Read the value from the throttle pin A0
        if (val < 290)
        {
            pedalon(); // throttle is not used, throttle voltage below minimum
        }
        else
        {
            torqueval = 76+(val-290)*164/287; // your experimental formulae
            analogWrite(A5, torqueval);
            pulse = true;
            if (currentMillis > (startMillis + pulseon)) pulse = false;
            if (currentMillis > (startMillis + pulseon + pulseoff)) startMillis = millis();     // reset timer clock
            if (pulse) digitalWrite(LED_BUILTIN, HIGH);
            else digitalWrite(LED_BUILTIN, LOW);
        }

    }
}

void pedalon()    //Called when throttle is not used
{
    analogWrite(A5, analogRead(A3)); // you need to check synthesised torque with a multitester
        if (digitalRead(2) == HIGH) digitalWrite(LED_BUILTIN, HIGH);
    else digitalWrite(LED_BUILTIN, LOW);
}
 

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
So an update on the code, I have tested the code you proposed but I had to make some modifications to the torqueval as your code was returning some negative values on certain occasions.

I initially tested via serial debugger everything looks ok no sign of errors. I turned to testing on the bike and here are the results:

1.
On initial power up - no errors (which is good).

The throttle code seems to work fine however I do feel like the motor is being pushed to go faster but stays at maximum 15mph. I can move the throttle up and down without any issues. However when I release the throttle (let go of the throttle) I get an error 22. So maybe we need to provide a keep alive in the code at the point of throttle release. Which is either in the digitalWrite LOW or when we fall in the val <290 statement but are not pedalling. It could be that when we are not throttling we need to provide a minimum torque value. I'll have to investigate further to work this out. But overall its looking good and close to completion in terms of throttle code.

2.
The moment I start pedalling the motor kicks in then suddenly stops and we get an error 21 so like you said I will need to check the torque with the multimeter. What should this reading be?

I'll aim to do further tests later and over the weekend.
 

Woosh

Trade Member
May 19, 2012
20,225
16,819
Southend on Sea
wooshbikes.co.uk
Motor stays at 15mph: that's built into the controller, you can't derestrict it with your code.

However when I release the throttle (let go of the throttle) I get an error 22. So maybe we need to provide a keep alive in the code at the point of throttle release.
when you release the throttle, it will switch over to pedalon().

Error 21 and error 22: they are caused by your pedalon() function.

1. measure the output voltage of the torque sensor at rest.
2. measure the output voltage of your LED pin.
3. measure the output voltage of the cadence sensor when high
4. measure the output voltage of the cadence sensor when low
 

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
Motor stays at 15mph: that's built into the controller, you can't derestrict it with your code.


when you release the throttle, it will switch over to pedalon().

Error 21 and error 22: they are caused by your pedalon() function.

1. measure the output voltage of the torque sensor at rest.
2. measure the output voltage of your LED pin.
3. measure the output voltage of the cadence sensor when high
4. measure the output voltage of the cadence sensor when low
1. Torque Sensor at rest from pin A3 = 3.84
2. LED pin 13 = 0.01 (this seems odd might need to re check this)
3. Cadence Sensor on HIGH pin D2 (the cadence sensor from the pedal) = 0.18
4. Cadence Sensor on LOW pin D2 (the cadence sensor from the pedal) = 0.13

I hope I measured the right pins.
 

Woosh

Trade Member
May 19, 2012
20,225
16,819
Southend on Sea
wooshbikes.co.uk
they don't look right.
A2: Put a 11kilo Ohm resistor between A2 and ground to see if the reading changes.
D2: Turn the cranks very slowly to see if the voltage on D2 rises and falls.
Until you get this signal D2 right, you will copy random value onto D13 and soon after get error 21 or 22.
 

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
they don't look right.
A2: Put a 11kilo Ohm resistor between A2 and ground to see if the reading changes.
D2: Turn the cranks very slowly to see if the voltage on D2 rises and falls.
Until you get this signal D2 right, you will copy random value onto D13 and soon after get error 21 or 22.
Ive rechecked the pin values they are as follows:

1. Pin D2 (the green wire from the pas sensor) at rest is 0.14 (when I start pedalling it goes up to 1.99 followed by error 22)
2. Pin D13 (the green wire to the controller) at rest is 0.01 (when I start pedalling it goes up to 1.86 followed by error 22)
3. Pin A3 (yellow wire from PAS) and A5 (yellow wire to the controller) at rest is 3.86.

I hope this helps.
 

Woosh

Trade Member
May 19, 2012
20,225
16,819
Southend on Sea
wooshbikes.co.uk
1. Pin D2 (the green wire from the pas sensor) at rest is 0.14 (when I start pedalling it goes up to 1.99 followed by error 22)
that's where the problem lies. The signal is not high enough during pulse on to translate it correctly onto D13.
Connect the green wire to an analog input pin and set up the debugger to read the input voltage while turning the cranks very slowly. You need to work out when to switch D13 to high.
 

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
that's where the problem lies. The signal is not high enough during pulse on to translate it correctly onto D13.
Connect the green wire to an analog input pin and set up the debugger to read the input voltage while turning the cranks very slowly. You need to work out when to switch D13 to high.
ok I’ve switched it to Analog Pin 2 and found the following:

1. Pin A2 at rest was 3.78 (on multimeter)
2. As I cranked the pedals the value went down to a range of 1.8-2.0

I also noticed as soon as I powered on the bike with this configuration the motored turned straight away without me turning the pedals or using the throttle.
 

Woosh

Trade Member
May 19, 2012
20,225
16,819
Southend on Sea
wooshbikes.co.uk
I need the analog reading range as seen on A2, you did find it with the throttle, min-max (throttle 290-577).
After that, modify the pedalon() to this:

Code:
void pedalon()    //Called when throttle is not used
{
    int cadence_threshold = 290;
    analogWrite(A5, analogRead(A3)); // you need to check synthesised torque with a multitester
        if (analogRead(A2) > cadence_threshold) digitalWrite(LED_BUILTIN, HIGH);
    else digitalWrite(LED_BUILTIN, LOW);
}
 

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
I need the analog reading range as seen on A2, you did find it with the throttle, min-max (throttle 290-577).
After that, modify the pedalon() to this:

Code:
void pedalon()    //Called when throttle is not used
{
    int cadence_threshold = 290;
    analogWrite(A5, analogRead(A3)); // you need to check synthesised torque with a multitester
        if (analogRead(A2) > cadence_threshold) digitalWrite(LED_BUILTIN, HIGH);
    else digitalWrite(LED_BUILTIN, LOW);
}
I’ll give this a try tomorrow.
 

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
So I have attempted to get a reading via the serial monitor by turning the cranks slowly and the following was the behaviour:

1) At initial power on the value returned in the serial debugger for the PAS Cadence (green wire) is 35, once I start to turn the cranks slowly it jumps to 1020 - 1023 the faster i turn the cranks the max value I get is 1023.

Also another observation of behaviour I'm not sure I mentioned it earlier but on boot up I am now getting error 21. To get pass this error on boot up I manually spin the rear wheel enough to give motion then power on the bike then I can use the throttle no problem. However the pedalling part still returns error 22.
 

Woosh

Trade Member
May 19, 2012
20,225
16,819
Southend on Sea
wooshbikes.co.uk
post your current code, I'll modify it for you with the current setup (cadence_in on pin A2).
 

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
post your current code, I'll modify it for you with the current setup (cadence_in on pin A2).
This is the latest code with cadence in on pin A2

Code:
// Experimenting
// PAS Cadence Input Pin - A2 (Analog)
// PAS Torque Input Pin - A3 (Analog)
// Throttle Input Pin - A0 (Analog)
// Controller Cadence Output Pin - D13 (Digital)
// Controller Torque Output Pin - A5 (Analog)

int pulseoff = 100; // milliseconds when synthesised cadence is low
int pulseon = 66;   // milliseconds when synthesised cadence is high 6Hz
int val = 0;

bool pulse = false;
bool throttle = false;
int throttleperc = 0;
int torqueperc = 0;
int torqueval = 76;
//int pedalin = 2;
unsigned long startMillis;
unsigned long currentMillis;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(A3, INPUT); //PASTorquePin
  pinMode(A5, OUTPUT); //ControllerTorquePin
  pinMode(A2, INPUT); //PASCadencePin
  digitalWrite(LED_BUILTIN, LOW);
  startMillis = millis();
  Serial.begin(9600);
}

void loop() {
    if (millis()>currentMillis) // run once every millisecond
    {
        currentMillis = millis();
        val = analogRead(A0); // Read the value from the throttle pin A0
        throttleperc = ((float)(val - 290) / (float)577) * 100;
        torqueperc = 164 * ((float)(val - 290) / (float)577);
        torqueval = torqueval + torqueperc;
        if (val < 290)
        {
            pedalon(); // throttle is not used, throttle voltage below minimum
        }
        else
        {
            //torqueval = 76+(val-290)*164/287; // your experimental formulae
            analogWrite(A5, torqueval);
            pulse = true;
            if (currentMillis > (startMillis + pulseon)) pulse = false;
            if (currentMillis > (startMillis + pulseon + pulseoff)) startMillis = millis();     // reset timer clock
            if (pulse) digitalWrite(LED_BUILTIN, HIGH);
            else digitalWrite(LED_BUILTIN, LOW);
        }

    }
}

void pedalon()    //Called when throttle is not used
{
    //int pascadence = 0;
    //int pastorque = 0;
    //pascadence = analogRead(A2); // Read the value from the PASCadence Pin
    //pastorque = analogRead(A3); // Read the value from the PASTorque Pin
    //Serial.print("Pas Cadence value is ");
    //Serial.print(pascadence);
    //Serial.print(" PAS Torque value is ");
    //Serial.print(pastorque);
    //Serial.println("");
    int cadence_threshold = 1020;
    analogWrite(A5, analogRead(A3)); // you need to check synthesised torque with a multitester
        if (analogRead(A2) > cadence_threshold) digitalWrite(LED_BUILTIN, HIGH);
    else digitalWrite(LED_BUILTIN, LOW);
}
Ignore the commented out section I used for the serial debugger. I was also experimenting with the pedalon function.
 

Woosh

Trade Member
May 19, 2012
20,225
16,819
Southend on Sea
wooshbikes.co.uk
Have you tried with a smaller value for cadence_threshold?

for example int cadence_threshold = 500;

PS: I think you also need to add a 1kOhm load resistor between A2 and GND to stabilise the readout.
The controller PAS pin usually has a load resistor on it.
 
Last edited:

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
Have you tried with a smaller value for cadence_threshold?

for example int cadence_threshold = 500;

PS: I think you also need to add a 1kOhm load resistor between A2 and GND to stabilise the readout.
The controller PAS pin usually has a load resistor on it.
I’ll give this a try tomorrow both the lower threshold and the ohm resister what readings would you be looking for?
 

Woosh

Trade Member
May 19, 2012
20,225
16,819
Southend on Sea
wooshbikes.co.uk

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
repeateable readings cycling between zero and around 1000 when moving the cranks very slowly.
I haven't had a chance to test this on the bike but I ran it through the serial debugger with the 1kOhm resister between A2 and GND and I get a fluctuation of values (see below):

Pas Cadence value is 473
Pas Cadence value is 578
Pas Cadence value is 562
Pas Cadence value is 368
Pas Cadence value is 648
Pas Cadence value is 543
Pas Cadence value is 395
Pas Cadence value is 680
Pas Cadence value is 504
Pas Cadence value is 418
Pas Cadence value is 699
Pas Cadence value is 467
Pas Cadence value is 447
Pas Cadence value is 699
Pas Cadence value is 435
Pas Cadence value is 492
Pas Cadence value is 682
Pas Cadence value is 405
Pas Cadence value is 536
Pas Cadence value is 663
Pas Cadence value is 382

I think the lowest value i saw was 217. I'll test this on the bike tomorrow with the debugger and without.

I also noticed something which i think is worth sharing so the PAS is connected to the Controller via this connectors in the photos i shared in an earlier post. When I looked into the connector at the Controller end I noticed something strange with the Pins inside the connector it looked like the pin connected to the red wire was put in upside down. (I will try to share a photo of this tomorrow to better explain this). So after I noticed this I simply joined the wires (my wires i have made for the plug and play) from the PAS to the Controller without an Arduino in the middle and I powered on the bike and got an error 21 straight away. Which I thought was strange as this is exactly what the connectors do right? So then i plugged the original connectors back and all was fine. This could be a red herring but i thought i'd mention it. I'll post a picture of the connector with the upside down pin tomorrow.
 

Woosh

Trade Member
May 19, 2012
20,225
16,819
Southend on Sea
wooshbikes.co.uk
I suggest you keep the load resistor for now and test various value of cadence_threshold between 300-600 to see if the error goes away.
 

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
So I tested this on my bike with the resister and observed the following.

1. I can power on the bike with no errors.
2. I can start the throttle from standstill and it seems to work. Once I let go of the throttle I get error 22 (which we know from before).
3. I can start to peddle but even the slightest turn of the cranks results in the motor going full speed rather than a gradual climb. After about 5 seconds I get error 22.

do I keep playing with the cadence reading and keep adjusting the threshold until I get a smooth transition from the pedals?

Also I wanted to share the photo of the connector where the PAS connects to the Controller here you can see one of the pins doesn’t look like it’s inserted correctly or it is that way for a reason.80840D53-9B56-4B7B-8244-E8A101A2F426.jpeg

I also wanted to share the diagram of the wiring setup.

41085
 
Last edited:

Advertisers