How to add a throttle to a Carrera Vengeance E Spec

Woosh

Trade Member
May 19, 2012
19,407
16,387
Southend on Sea
wooshbikes.co.uk
No, if it turn on the pedals there is no blinking.
No blinking means the arduino does not see changes on pin A2.
It should not matter that the LCD shows error 21, the cadence input should still shows that you turn the cranks.

can you check with a multitester if the voltage on A2 cycles between 0V and 4V when you rotate the cranks very slowly?
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
The data i got when turning the cranks slowly i see it rising from 1.13v (or 1.15) to 3.83v but it will remain on 3.83v when there is no motion on the crank until you slowly pedal it again and you see the same result
 

Woosh

Trade Member
May 19, 2012
19,407
16,387
Southend on Sea
wooshbikes.co.uk
when the voltage on A2 is low (1,15V), is the arduino LED off?
when the voltage on A2 is high (3,8V), is the arduino LED on?
 

Woosh

Trade Member
May 19, 2012
19,407
16,387
Southend on Sea
wooshbikes.co.uk
thank you for that, can you amend A0 to A2 on this line:

if (analogRead(A2) < 500) digitalWrite(LED_BUILTIN, LOW);

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)

unsigned long currentMillis;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(A2, INPUT); // PAS cadence Pin
  pinMode(A3, INPUT); //PASTorquePin
  pinMode(A5, OUTPUT); //ControllerTorquePin
  digitalWrite(LED_BUILTIN, LOW);
}


void loop() {   // does cadence input cause errors?
    if (millis()>currentMillis) // run once every millisecond
    currentMillis = millis();
    {
    if (analogRead(A2) < 500) digitalWrite(LED_BUILTIN, LOW);
    else  digitalWrite(LED_BUILTIN, HIGH);   // blink LED
    }
}
 

Woosh

Trade Member
May 19, 2012
19,407
16,387
Southend on Sea
wooshbikes.co.uk
that's good, we are nearly there.
did you see any error?
assuming there was no error, let's run this code to start the motor in pedal mode:
analogWrite(A5, analogRead(A3)/4); // replicate torque input, analogRead 10-bit. analogWrite 8-bit.

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)

unsigned long currentMillis;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(A2, INPUT); // PAS cadence Pin
  pinMode(A3, INPUT); //PASTorquePin
  pinMode(A5, OUTPUT); //ControllerTorquePin
  digitalWrite(LED_BUILTIN, LOW);
}


void loop() {   // does cadence input cause errors?
    if (millis()>currentMillis) // run once every millisecond
    currentMillis = millis();
    {
    analogWrite(A5, analogRead(A3)/4);
    if (analogRead(A2) < 500) digitalWrite(LED_BUILTIN, LOW);
    else  digitalWrite(LED_BUILTIN, HIGH);   // blink LED
    }
}
 
Last edited:

Evian1040

Pedelecer
Jul 7, 2020
75
9
Error 21 on startup as usual, but i can see from the code we are not passing anything to A5 so its natural.
EDIT: I'll try that updated code.
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
Error 21 on startup.
Went to check on the multimeter and it gave out 0.77v on the torque but i know that if i power it up normally it starts at 3.xx volts and then it fluctuates based on the cadence.
Can we try with 1.5v as it looks to be the bare minimum for the cadence?
 

Woosh

Trade Member
May 19, 2012
19,407
16,387
Southend on Sea
wooshbikes.co.uk
yes, I think you are right on this.
Let's test this line:
analogWrite(A5, analogRead(A3)/4);

change it to:
analogWrite(A5, 75 + analogRead(A3)/4);
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
Tried it, still on error 21.
Plugged the controller torque to the 3.3v to make sure i was right and yep it didn't show error 21. As a side thing i did pedal to see if it worked and it did kick the motor as expected but of course it went to error 22 since we were giving 3.3v and the hz was not what the controller wanted.
Is there a way to start with 3.3v but then lower it to 1.5v when any function is introduced and continue at that voltage?
 

Woosh

Trade Member
May 19, 2012
19,407
16,387
Southend on Sea
wooshbikes.co.uk
Tried it, still on error 21.
Plugged the controller torque to the 3.3v to make sure i was right and yep it didn't show error 21. As a side thing i did pedal to see if it worked and it did kick the motor as expected but of course it went to error 22 since we were giving 3.3v and the hz was not what the controller wanted.
Is there a way to start with 3.3v but then lower it to 1.5v when any function is introduced and continue at that voltage?
So to remove error 21, the controller expects to see 3.3V now and then when the motor isn't running.

I'll post updated code a little later.
 

Woosh

Trade Member
May 19, 2012
19,407
16,387
Southend on Sea
wooshbikes.co.uk
how to set pastorque to 3.5V if you stop pedalling:

Code:
// Experimenting v5
// 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 = 150; // milliseconds when synthesised cadence is low
int pulseon = 100;   // milliseconds when synthesised cadence is high 6Hz
int val = 0;
int cadencepulselength = 0; // in milliseconds
bool cadencestate = false;
bool pulse = false;
bool throttle = false;
int throttleperc = 0;
int torqueperc = 0;
int torqueval = 76;

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();

    // update cadencepulselength
    if (cadencepulselength < 1000) cadencepulselength++;

    if (cadencestate)
    {
        if (analogRead(A2) < cadence_threshold) cadencepulselength = 0;
        cadencestate = false;
     }
    if (analogRead(A2) > cadence_threshold) cadencestate = true;

        val = analogRead(A0); // Read the value from the throttle pin A0
        torqueval = 0;
        if (val < 290)
        {
                // I'm pedalling
                int pascadence = 0;
                int pastorque = 0;
                pascadence = analogRead(A2); // Read the value from the PASCadence Pin
                pastorque = 75 + analogRead(A3)/4; // experimental
                if (pastorque > 200) pastorque =200;  // experimental
               if (cadencepulselength > 990) pastorque = 200; // set rest value to 3.3V if no pulse detected in 1 sec
                analogWrite(A5, pastorque); // you need to check synthesised torque with a multitester
                if (analogRead(A2) > cadence_threshold) digitalWrite(LED_BUILTIN, HIGH);
                else digitalWrite(LED_BUILTIN, LOW);
        }
        else
        {
            //I'm using the throttle
            torqueval = (308 + val * (880-308)/(577-290)); // experimental
            analogWrite(A5, torqueval/4);
            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);
        }

    }
}
the loop increments cadencepulselength until it is reset by a transition on the cadence pin from high to low.
If the cadence drops below 1Hz, pastorque is set to 3.5V.
 

Woosh

Trade Member
May 19, 2012
19,407
16,387
Southend on Sea
wooshbikes.co.uk
amended code:


Code:
// Experimenting v6
// 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)// Experimenting

const byte ThrottlePin = A0;
const byte CadenceInputPin = A2;
const byte TorqueInputPin = A3;
const byte TorqueOutputPin = A5;
const byte CadenceOutputPin = 13;

int pulseoff = 100; // milliseconds when synthesised cadence is low
int pulseon = 66;   // milliseconds when synthesised cadence is high 6Hz
int cadencepulselength = 0; // in milliseconds
bool cadencestate = false;
bool pulse = false;
unsigned long startMillis;
unsigned long currentMillis;

void setup() {
  pinMode(CadenceOutputPin, OUTPUT);
  pinMode(TorqueInputPin, INPUT);
  pinMode(TorqueOutputPin, OUTPUT); //ControllerTorquePin
  pinMode(CadenceInputPin, INPUT); //PASCadencePin
  digitalWrite(TorqueOutputPin, LOW);
  startMillis = millis();
  // Serial.begin(9600);
}

void loop() {
    if (millis()>currentMillis) // run once every millisecond
    {
        currentMillis = millis();
        // increment cadencepulselength
        if (cadencepulselength < 1000) cadencepulselength++;

        if (cadencestate)
        {
            // on cadencestate transtion from high to low, reset cadencepulselength
            if (analogRead(CadenceInputPin) < 512)
            {
               cadencepulselength = 0;
               cadencestate = false;
            }
        }
        if (analogRead(CadenceInputPin) > 512) cadencestate = true;

        // check throttle

        int torqueval = analogRead(ThrottlePin) / 4; // Read the value from the throttle pin A0
        if (torqueval < 75)  // disable throttle if voltage < 1.5V
        {
        // I'm pedalling
        int pastorque = analogRead(TorqueInputPin)/4; // torque input A3
        // if I'm stopping for more than 1 second, set A5 to 3.5V to stop error 21
        // if (cadencepulselength > 990) pastorque = 200;

        // replicate torque and cadence
        analogWrite(TorqueOutputPin, pastorque);
        if (analogRead(CadenceInputPin) > 512) digitalWrite(CadenceOutputPin, HIGH);
        else digitalWrite(CadenceOutputPin, LOW);
    }
    else
    {
        //I'm using the throttle

        analogWrite(TorqueOutputPin, torqueval);
        pulse = true;
        if (currentMillis > (startMillis + pulseon)) pulse = false;
        if (currentMillis > (startMillis + pulseon + pulseoff)) startMillis = millis();     // reset timer clock
        if (pulse) digitalWrite(CadenceOutputPin, HIGH);
        else digitalWrite(CadenceOutputPin, LOW);
    }
  }
}
 
Last edited:

Evian1040

Pedelecer
Jul 7, 2020
75
9
When compiling the code it gives out "D13 was not declared on the scope", i changed it to 13 and then another error comes up with "'pulse' was not declared in this scope", so i added "bool pulse = false;" (no clue if i should have added that.
Compiled it after that and when testing it produces error 21, went to check the multimeter on A3 to A5 and the voltage was 3.4v so by default that should of worked. I then noticed a slight delay on bootup even without the bootloader on the nano so i applied external power to the nano so the code initialized and then i powered on the bike but it still gives me error 21. (of course if i disconnect the torque from the nano and just plug it directly, no error 21 is produced.)
 

Woosh

Trade Member
May 19, 2012
19,407
16,387
Southend on Sea
wooshbikes.co.uk
I amended the code from your findings.
did the throttle work?
can you comment out this line and test again:
// if (cadencepulselength > 990) pastorque = 200;
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
With the previous one no, made the change as requested as still got error 21.
Wired directly the torque to bypass error 21 and tried the throttle and it didn't work.
Pedaling didn't work as well.
 

Woosh

Trade Member
May 19, 2012
19,407
16,387
Southend on Sea
wooshbikes.co.uk
Pedaling didn't work as well.
do you think that the problem is in replicating the cadence? or is the pastorque too low?
when using the throttle, did the LED blink?
can you also check the voltage on A5 when maxing the throttle?
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
do you think that the problem is in replicating the cadence? or is the pastorque too low?
Error 21 will only happen when the controller thinks that power to the PAS has been discontinued or the PAS is outputting an un-syncronized power flow. The controller's boot-up sequence recognizes torque as priority so the cadence on startup is ignored until the controller sees torque power while pedaling.

when using the throttle, did the LED blink?
Yep

can you also check the voltage on A5 when maxing the throttle?
Multimeter reads 3.84v

Edit: Side note, i wished the bike was just 3 pin... That would be a lot easier.
 

Advertisers