Skip to content

Tests

Battery power

  1. Powering Pico with a 12V battery and voltage measurement
  2. Integration test on V0.1 board
  3. Battery voltage drop
  4. Maximum current

1. Powering Pico with a 12V battery and voltage measurement

  1. Components:

    • 12 V 6000 mAh Li-ion battery
    • LM2596 buck converter module
    • Raspberry Pi Pico
    • SH1106 OLED
    • R1 (47 kΩ)
    • R2 (10 kΩ)
    • Schottly barrier diode (40V 1A)
  2. Overview:

    1. Voltage measurement:
      1. Voltage divider reduces battery voltage up to 12 V to 3.3 V or less
        • R1 = 47 kΩ (because 33 kΩ was not available), R2 = 10 kΩ
        • Vadc = Vin × (R2 / (R1 + R2)) = 12 × (10 / 57) ≈ 2.10V
      2. Pico's ADC pin measures voltage (up to 3.3 V)
        • Vadc: GP26 (ADC0) → VBAT = Vadc × 5.7 = Original voltage
        • VSYS: GPIO 29 (ADC3)
      3. The OLED displays the measured VBAT and VSYS voltage
    2. Voltage regulation and Pico power supply
      1. Regulated 12V battery power to 5V using an LM2596 buck converter
        • Output voltage: set to 5V
      2. Supplied it to the Pico through the VSYS pin
        • VSYS: Pico’s main system input, supports 1.8V to 5.5V
        • Pico automatically selects the higher of USB or VSYS when both are connected
        • Schottky barrier diode 40V1A 1N5819 to prevent one power source from back-feeding the other (Pico also equip onboard)
        • Lower forward voltage drop compared to noraml diodes
  3. Wiring:

  4. Code:

    Arduino code
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    // Minimal VBAT & VSYS Voltage Test for Raspberry Pi Pico + OLED display
    uint16_t raw;   // Raw ADC reading (0–4095 for 12-bit resolution)
    
    #include <U8g2lib.h>
    
    int xPosText = 128;  // Initial horizontal position for the text
    int xPosRect = 0;    // Initial horizontal position for the rectangles
    
    // SH1106 128x64 SPI constructor (4-wire software SPI)
    #define OLED_CS   9
    #define OLED_DC   13
    #define OLED_RST  12
    #define OLED_SCK  10
    #define OLED_MOSI 11
    
    U8G2_SH1106_128X64_NONAME_F_4W_SW_SPI u8g2(
        U8G2_R0, 
        /* clock=*/ OLED_SCK, 
        /* data=*/ OLED_MOSI, 
        /* cs=*/ OLED_CS, 
        /* dc=*/ OLED_DC, 
        /* reset=*/ OLED_RST
    );
    
    void setup() {
        Serial.begin(14101);           // Initialize serial communication
        analogReadResolution(12);      // Use 12-bit ADC (0–4095)
        pinMode(26, INPUT);            // GPIO26 for VBAT (external divider)
        pinMode(29, INPUT);            // GPIO29 for VSYS (internal divider)
        pinMode(LED_BUILTIN, OUTPUT);  // Built-in LED for activity indication
        u8g2.begin();                  // Initialize OLED
    }
    
    // Function: readVBAT
    float readVBAT() {
        uint16_t raw = analogRead(26);
        float v_adc = raw * 3.3 / 4095.0;     // ADC to voltage
        float v_bat = v_adc * 5.7;            // Divider compensation (adjust if needed)
    
        Serial.print("ADC26: ");  
        Serial.print(raw);
        Serial.print("  |  VBAT: ");  
        Serial.print(v_bat, 3);
        Serial.println(" V");
    
        return v_bat;
    }
    
    // Function: readVSYS
    float readVSYS() {
        uint16_t raw = analogRead(29);
        float v_adc = raw * 3.3 / 4095.0;
        float v_sys = v_adc * 3.0;            // Typical divider factor for VSYS ADC input
    
        Serial.print("ADC29: ");  
        Serial.print(raw);
        Serial.print("  |  VSYS: ");  
        Serial.print(v_sys, 3);
        Serial.println(" V");
    
        return v_sys;
    }
    
    void loop() {
        digitalWrite(LED_BUILTIN, HIGH);
    
        float v_bat = readVBAT();
        float v_sys = readVSYS();
    
        // OLED display
        u8g2.clearBuffer();
        u8g2.setFont(u8g2_font_10x20_tr);
    
        char buf1[16];
        snprintf(buf1, sizeof(buf1), "VBAT: %.2fV", v_bat);
        u8g2.drawStr(0, 20, buf1);
    
        char buf2[16];
        snprintf(buf2, sizeof(buf2), "VSYS: %.2fV", v_sys);
        u8g2.drawStr(0, 40, buf2);
    
        u8g2.sendBuffer();
    
        digitalWrite(LED_BUILTIN, LOW);
        delay(1000);
    }
    
  5. Outocome:

    USB power Battery power VBAT VSYS Note
    Connected Disconnected 0.2 V 5.0 V USB power = 5 V
    Connected Connected 12.0 V 5.0 V USB power is higher → 5.0 V
    Disconnected Connected 12.0 V 4.8 V Battery power was dropped by the diode (The multimeter shows 5.07 V before the diode)

    Accuracy:

    Condition Multimeter (Actual Voltage) ADC Reading (Measured)
    Battery disconnected 0.00 V 0.16 V
    Battery connected 11.71 V 11.98 V

    According to ChatGPT:

    • 0.2-0.3 V difference is reasonable (≈2–3 %).
    • Possible causes:
      • Resistor tolerance (±1–5 %)
      • ADC inaccuracy and noise
      • VREF variation (3.25–3.35 V)
      • Breadboard or wire resistance
      • Lack of calibration
    • Possible improvements (optional):
      • Use precision resistors (1 % or better)
      • Add a capacitor (0.1 µF–1 µF) across ADC input for stability
      • Measure actual VREF and adjust in code
      • Average multiple ADC readings for smoother output

    Calibration:

    • v_bat = v_adc * 5.7 - 0.25;
    • or v_bat = v_adc * 5.7 * 0.98; ?

2. Integration test on V0.1 board

  1. Overview:

    • Integrated the 12V battery power, buck converter for 5V line, measurement of battery and VSYS voltage and displaying it on SPI OLED.
    • Wired using jump wires and bread board. Messy.
    • 12V line for buck converter and voltage divider is connected to the capacitors on the V0.1 board using alligator clips.
    • To test without serial over USB, two limit switches were used to move X and Y. Code was added to generate G-code while limit X or Y was pressed, but the rest of the code remained unchanged.
    • While USB connected, it works as it is.
  2. Wiring:

    Beware of short circuits

  3. Code:

    Arduino code
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    //////////////////// for Voltage measurement
    uint16_t raw;   // Raw ADC reading (0–4095 for 12-bit resolution)
    
    #include <U8g2lib.h>
    
    int xPosText = 128;  // Initial horizontal position for the text
    int xPosRect = 0;    // Initial horizontal position for the rectangles
    
    // SH1106 128x64 SPI constructor (4-wire software SPI)
    #define OLED_SCK  6 //10
    #define OLED_MOSI 7 //11
    #define OLED_RST  4 //12, 8
    #define OLED_DC   5 //13, 9
    #define OLED_CS   8 //9, 10
    
    U8G2_SH1106_128X64_NONAME_F_4W_SW_SPI u8g2(
        U8G2_R0, 
        /* clock=*/ OLED_SCK, 
        /* data=*/ OLED_MOSI, 
        /* cs=*/ OLED_CS, 
        /* dc=*/ OLED_DC, 
        /* reset=*/ OLED_RST
    );
    
    //////////////////// for FP V0.1
    String generatedGcode = ""; // for test without SERIAL
    bool gcodeGenerated = false;
    
    #include <AccelStepper.h>
    #include <Servo.h>
    
    // Global position
    float currentX = 0;
    float currentY = 0;
    float currentR = 0;
    
    // Calibration: steps required to move 1mm or 1 degree
    const float STEPS_PER_MM_X = 17.95;  // 17.95, adjust based on mechanical setup
    const float STEPS_PER_MM_Y = 17.00;
    const float STEPS_PER_DEG_R = 45.50; // for rotation
    
    // Stepper motor pins
    const int STEP_PIN_RF = 19;
    const int DIR_PIN_RF  = 18;
    const int STEP_PIN_LF = 12;
    const int DIR_PIN_LF  = 13;
    const int STEP_PIN_RB = 17;
    const int DIR_PIN_RB  = 16;
    const int STEP_PIN_LB = 14;
    const int DIR_PIN_LB  = 15;
    
    // Microstepping control
    // Swap the EN pin to 9, omit MS1~3
    const int EN_PIN = 11; //9
    // const int MS1 = 6, MS2 = 7, MS3 = 8;
    
    // Servo Z-axis
    Servo servoZ;
    const int SERVO_PIN_Z = 22;
    int currentZ = 120; //
    
    // Limit switches
    const int LIMIT_X = 20; //28
    const int LIMIT_Y = 21; //27
    
    const int MAX_SPEED = 1500; //800
    const int ACCEL = 2000; //600
    const int HOMING_SPEED = 200;
    const int BACK_OFF_STEPS = 500;
    
    const unsigned long INACTIVITY_TIMEOUT = 5000; // ms
    unsigned long lastCommandTime = 0;
    
    String input = "";
    
    // Stepper drivers
    AccelStepper stepperRF(AccelStepper::DRIVER, STEP_PIN_RF, DIR_PIN_RF);
    AccelStepper stepperLF(AccelStepper::DRIVER, STEP_PIN_LF, DIR_PIN_LF);
    AccelStepper stepperRB(AccelStepper::DRIVER, STEP_PIN_RB, DIR_PIN_RB);
    AccelStepper stepperLB(AccelStepper::DRIVER, STEP_PIN_LB, DIR_PIN_LB);
    
    void setup() {
        // for Voltage measurement
        Serial.begin(14101);           // Initialize serial communication
        analogReadResolution(12);      // Use 12-bit ADC (0–4095)
        pinMode(26, INPUT);            // GPIO26 for VBAT (external divider)
        pinMode(29, INPUT);            // GPIO29 for VSYS (internal divider)
        pinMode(LED_BUILTIN, OUTPUT);  // Built-in LED for activity indication
        u8g2.begin();                  // Initialize OLED
    
        // for FP V0.1
        pinMode(EN_PIN, OUTPUT);
        // pinMode(MS1, OUTPUT); pinMode(MS2, OUTPUT); pinMode(MS3, OUTPUT);
        pinMode(LIMIT_X, INPUT_PULLUP);
        pinMode(LIMIT_Y, INPUT_PULLUP);
    
        digitalWrite(EN_PIN, LOW); // enable drivers
        // digitalWrite(MS1, HIGH); digitalWrite(MS2, HIGH); digitalWrite(MS3, HIGH);
    
        stepperRF.setMaxSpeed(MAX_SPEED); stepperRF.setAcceleration(ACCEL);
        stepperLF.setMaxSpeed(MAX_SPEED); stepperLF.setAcceleration(ACCEL);
        stepperRB.setMaxSpeed(MAX_SPEED); stepperRB.setAcceleration(ACCEL);
        stepperLB.setMaxSpeed(MAX_SPEED); stepperLB.setAcceleration(ACCEL);
    
        servoZ.attach(SERVO_PIN_Z, 500, 2400);
        servoZ.write(currentZ);
    
        lastCommandTime = millis();
    
        Serial.println("Ready for G-code like G1 X100 Y0 Z90 or G28");
        Serial.print("Initial position: X="); Serial.print(currentX);
        Serial.print(" Y="); Serial.print(currentY);
        Serial.print(" R="); Serial.println(currentR);
    }
    
    //////////////////// Minimal VBAT & VSYS Voltage Test + OLED display
    // Function: readVBAT
    float readVBAT() {
        uint16_t raw = analogRead(26);
        float v_adc = raw * 3.3 / 4095.0;     // ADC to voltage
        float v_bat = v_adc * 5.7;            // Divider compensation (adjust if needed)
    
        Serial.print("ADC26: ");  
        Serial.print(raw);
        Serial.print("  |  VBAT: ");  
        Serial.print(v_bat, 3);
        Serial.println(" V");
    
        return v_bat;
    }
    
    // Function: readVSYS
    float readVSYS() {
        uint16_t raw = analogRead(29);
        float v_adc = raw * 3.3 / 4095.0;
        float v_sys = v_adc * 3.0;            // Typical divider factor for VSYS ADC input
    
        Serial.print("ADC29: ");  
        Serial.print(raw);
        Serial.print("  |  VSYS: ");  
        Serial.print(v_sys, 3);
        Serial.println(" V");
    
        return v_sys;
    }
    
    //////////////////// FP 0.1 + 
    
    void homeAxes() {
        Serial.println("Starting zeroing routine...");
    
        stepperRF.setMaxSpeed(HOMING_SPEED);
        stepperLF.setMaxSpeed(HOMING_SPEED);
        stepperRB.setMaxSpeed(HOMING_SPEED);
        stepperLB.setMaxSpeed(HOMING_SPEED);
    
        // --- Homing X ---
        stepperRF.setSpeed(100);  // Set speed directly (positive or negative)
        stepperLF.setSpeed(-100);
        stepperRB.setSpeed(-100);
        stepperLB.setSpeed(100);
    
        while (digitalRead(LIMIT_X) == HIGH) {
            stepperRF.runSpeed();
            stepperLF.runSpeed();
            stepperRB.runSpeed();
            stepperLB.runSpeed();
        }
    
        Serial.println("LIMIT_X triggered. X position zeroed.");
        delay(200);
    
        // --- Homing Y ---
        stepperRF.setSpeed(-100);  // Set speed directly (positive or negative)
        stepperLF.setSpeed(-100);
        stepperRB.setSpeed(-100);
        stepperLB.setSpeed(-100);
    
        while (digitalRead(LIMIT_Y) == HIGH) {
            stepperRF.runSpeed();
            stepperLF.runSpeed();
            stepperRB.runSpeed();
            stepperLB.runSpeed();
        }
    
        // --- Stop and zero
        stepperRF.stop(); stepperLF.stop(); stepperRB.stop(); stepperLB.stop();
        stepperRF.setCurrentPosition(0); stepperLF.setCurrentPosition(0);
        stepperRB.setCurrentPosition(0); stepperLB.setCurrentPosition(0);
    
        Serial.println("LIMIT_Y triggered. Y position zeroed.");
        delay(200);
    
        // Reset relative position 0
        currentX = 0;
        currentY = 0;
        currentR = 0;
    
        // Back off
        stepperLF.move(BACK_OFF_STEPS);
        stepperRB.move(BACK_OFF_STEPS);
        while (stepperRF.isRunning() || stepperLF.isRunning() || stepperRB.isRunning() || stepperLB.isRunning()) {
            stepperRF.run(); stepperLF.run(); stepperRB.run(); stepperLB.run();
        }
    
        Serial.println("Zeroing complete");
    }
    
    //////////////////// for Battery test without SERIAL
    void feedGeneratedGcode(int x_mm, int y_mm) {
        String cmd = "G1 X" + String(x_mm) + " Y" + String(y_mm);
        parseAndExecute(cmd);
    }
    
    //////////////////// FP V0.1
    void parseAndExecute(String cmd) {
        cmd.trim();
        cmd.toUpperCase();
        lastCommandTime = millis();
        digitalWrite(EN_PIN, LOW);
    
        if (cmd.startsWith("G28")) {
            homeAxes();
            return;
        }
    
        if (!cmd.startsWith("G1")) {
            Serial.println("Invalid or unsupported command.");
            return;
        }
    
        float targetX = currentX;
        float targetY = currentY;
        float targetR = currentR;
        int z = -1;
    
        int xIndex = cmd.indexOf('X');
        if (xIndex != -1) targetX = cmd.substring(xIndex + 1).toFloat();
    
        int yIndex = cmd.indexOf('Y');
        if (yIndex != -1) targetY = cmd.substring(yIndex + 1).toFloat();
    
        int rIndex = cmd.indexOf('R');
        if (rIndex != -1) targetR = cmd.substring(rIndex + 1).toFloat();
    
        int zIndex = cmd.indexOf('Z');
        if (zIndex != -1) z = cmd.substring(zIndex + 1).toInt();
    
        if (z != -1) {
            z = constrain(z, 0, 180);
            servoZ.write(z);
            currentZ = z;
            Serial.print("Servo moved to "); Serial.print(z); Serial.println(" degrees");
        }
    
        // Calculate deltas
        float deltaX = targetX - currentX;
        float deltaY = targetY - currentY;
        float deltaR = targetR - currentR;
    
        // --- 1. Move in X first ---
        if (deltaX != 0) {
            float deltaXSteps = deltaX * STEPS_PER_MM_X;
    
            long rfSteps = round(-deltaXSteps); // Only X
            long lfSteps = round( deltaXSteps);
            long rbSteps = round( deltaXSteps);
            long lbSteps = round(-deltaXSteps);
    
            stepperRF.move(rfSteps);
            stepperLF.move(lfSteps);
            stepperRB.move(rbSteps);
            stepperLB.move(lbSteps);
    
            while (stepperRF.isRunning() || stepperLF.isRunning() || stepperRB.isRunning() || stepperLB.isRunning()) {
                stepperRF.run(); stepperLF.run(); stepperRB.run(); stepperLB.run();
            }
    
            currentX = targetX; // Update position
        }
    
        // --- 2. Then move in Y ---
        if (deltaY != 0) {
            float deltaYSteps = deltaY * STEPS_PER_MM_Y;
    
            long rfSteps = round( deltaYSteps);
            long lfSteps = round( deltaYSteps);
            long rbSteps = round( deltaYSteps);
            long lbSteps = round( deltaYSteps);
    
            stepperRF.move(rfSteps);
            stepperLF.move(lfSteps);
            stepperRB.move(rbSteps);
            stepperLB.move(lbSteps);
    
            while (stepperRF.isRunning() || stepperLF.isRunning() || stepperRB.isRunning() || stepperLB.isRunning()) {
                stepperRF.run(); stepperLF.run(); stepperRB.run(); stepperLB.run();
            }
    
            currentY = targetY; // Update position
        }
    
        // --- 3. Optional rotation ---
        if (deltaR != 0) {
            float deltaRSteps = deltaR * STEPS_PER_DEG_R;
    
            long rfSteps = round(-deltaRSteps);
            long lfSteps = round( deltaRSteps);
            long rbSteps = round(-deltaRSteps);
            long lbSteps = round( deltaRSteps);
    
            stepperRF.move(rfSteps);
            stepperLF.move(lfSteps);
            stepperRB.move(rbSteps);
            stepperLB.move(lbSteps);
    
            while (stepperRF.isRunning() || stepperLF.isRunning() || stepperRB.isRunning() || stepperLB.isRunning()) {
                stepperRF.run(); stepperLF.run(); stepperRB.run(); stepperLB.run();
            }
    
            currentR = targetR; // Update rotation
        }
    
        Serial.println("Moved to:");
        Serial.print("X: "); Serial.print(currentX, 2); Serial.print(" mm");
        Serial.print(" | Y: "); Serial.print(currentY, 2); Serial.print(" mm");
        Serial.print(" | R: "); Serial.print(currentR, 2); Serial.println(" deg");
    
        Serial.print("Servo Z (degrees): ");
        Serial.println(currentZ);
    }
    
    void loop() {
        //////////////////// Voltage measurement
        digitalWrite(LED_BUILTIN, HIGH);
    
        float v_bat = readVBAT();
        float v_sys = readVSYS();
    
        // OLED display
        u8g2.clearBuffer();
        u8g2.setFont(u8g2_font_10x20_tr);
    
        char buf1[16];
        snprintf(buf1, sizeof(buf1), "VBAT: %.2fV", v_bat);
        u8g2.drawStr(0, 20, buf1);
    
        char buf2[16];
        snprintf(buf2, sizeof(buf2), "VSYS: %.2fV", v_sys);
        u8g2.drawStr(0, 40, buf2);
    
        u8g2.sendBuffer();
    
        digitalWrite(LED_BUILTIN, LOW);
        delay(1000);
    
        //////////////////// FP V0.1 Serial input
        while (Serial.available()) {
            char c = Serial.read();
            if (c == '\n' || c == '\r') {
                if (input.length() > 0) {
                    parseAndExecute(input);
                    input = "";
                    Serial.println("ok");
                }
            } else {
                input += c;
            }
        }
    
        // Sleep mode
        if (millis() - lastCommandTime > INACTIVITY_TIMEOUT) {
            digitalWrite(EN_PIN, HIGH); // disable drivers
        }
    
        //////////////////// without SERIAL
        // LIMIT_X → Move +5mm in X
        if (digitalRead(LIMIT_X) == LOW) {   // ====== Button pressed ======
            Serial.println("Manual: Move +10mm X");
            parseAndExecute("G1 X" + String(currentX + 10));
            delay(20); // debounce & slow repeat
        }
    
        // LIMIT_Y → Move +5mm in Y
        if (digitalRead(LIMIT_Y) == LOW) {   // ====== Button pressed ======
            Serial.println("Manual: Move +10mm Y");
            parseAndExecute("G1 Y" + String(currentY + 10));
            delay(20); // debounce
        }
    }
    
  4. Outcome:

    It demonstrated:

    • Using a 12V battery to power the motors
    • Stepping down 12V to 5V with a buck converter to supply power to Pico
    • Implementing voltage monitoring using a voltage-divider circuit
    • SPI OLED display
    • Ensuring the system can operate without a USB connection to the Pico

3. Battery voltage drop

  1. Overview:

    • To test how the battery voltage drops, VBAT value is recorded continuously
    • The program moves the machine forward, backward, right and left hand side continuously
    • The battery, the buck converter, breadboard and the OLED are attached on the machine somehow
    • Serial plotter displays voltage drop on PC
  2. Wiring:

  3. Code:

    Arduino code for test:

    Arduino code
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    //////////////////// Voltage & Stepper Battery Test
    
    #include <U8g2lib.h>
    #include <AccelStepper.h>
    #include <Servo.h>
    
    // -------------------- OLED 128x64 --------------------
    #define OLED_SCK  6
    #define OLED_MOSI 7
    #define OLED_RST  4
    #define OLED_DC   5
    #define OLED_CS   8
    
    U8G2_SH1106_128X64_NONAME_F_4W_SW_SPI u8g2(
    U8G2_R0, OLED_SCK, OLED_MOSI, OLED_CS, OLED_DC, OLED_RST
    );
    
    // -------------------- Stepper Pins --------------------
    const int STEP_PIN_RF = 19;
    const int DIR_PIN_RF  = 18;
    const int STEP_PIN_LF = 12;
    const int DIR_PIN_LF  = 13;
    const int STEP_PIN_RB = 17;
    const int DIR_PIN_RB  = 16;
    const int STEP_PIN_LB = 14;
    const int DIR_PIN_LB  = 15;
    const int EN_PIN = 11;
    
    // -------------------- Test Settings --------------------
    const int TEST_STEPS = 1000;         // 一往復あたりのステップ数
    const unsigned long MOVE_INTERVAL = 200; // ms
    bool testMode = false;
    
    // -------------------- Global --------------------
    AccelStepper stepperRF(AccelStepper::DRIVER, STEP_PIN_RF, DIR_PIN_RF);
    AccelStepper stepperLF(AccelStepper::DRIVER, STEP_PIN_LF, DIR_PIN_LF);
    AccelStepper stepperRB(AccelStepper::DRIVER, STEP_PIN_RB, DIR_PIN_RB);
    AccelStepper stepperLB(AccelStepper::DRIVER, STEP_PIN_LB, DIR_PIN_LB);
    
    unsigned long lastMoveTime = 0;
    int moveDirection = 1; // 1 = forward, -1 = backward
    
    unsigned long lastVBATread = 0;
    
    // -------------------- Voltage Measurement --------------------
    float readVBAT() {
        uint16_t raw = analogRead(26);
        float v_adc = raw * 3.3 / 4095.0;
        float v_bat = v_adc * 5.7 - 0.25;
        Serial.println(v_bat); // Serial Plotter用にVBATのみ出力
        // Serial.println(" V");
        return v_bat;
    }
    
    float readVSYS() {
        uint16_t raw = analogRead(29);
        float v_adc = raw * 3.3 / 4095.0;
        float v_sys = v_adc * 3.0;
        return v_sys;
    }
    
    // -------------------- Helper --------------------
    void runAllSteppers() {
        while (
            stepperRF.distanceToGo() != 0 ||
            stepperLF.distanceToGo() != 0 ||
            stepperRB.distanceToGo() != 0 ||
            stepperLB.distanceToGo() != 0
        ) {
            stepperRF.run();
            stepperLF.run();
            stepperRB.run();
            stepperLB.run();
        }
    }
    
    void setup() {
        Serial.begin(14101);
        analogReadResolution(12);
        pinMode(26, INPUT); // VBAT
        pinMode(29, INPUT); // VSYS
        pinMode(LED_BUILTIN, OUTPUT);
        pinMode(EN_PIN, OUTPUT);
        digitalWrite(EN_PIN, LOW);
    
        u8g2.begin();
    
        stepperRF.setMaxSpeed(1500); stepperRF.setAcceleration(2000);
        stepperLF.setMaxSpeed(1500); stepperLF.setAcceleration(2000);
        stepperRB.setMaxSpeed(1500); stepperRB.setAcceleration(2000);
        stepperLB.setMaxSpeed(1500); stepperLB.setAcceleration(2000);
    
        Serial.println("Type 'START' to begin test, 'STOP' to stop.");
    }
    
    // Movement pattern: 0=F, 1=R, 2=B, 3=L
    int movementStep = 0;
    
    void moveForward() {
        stepperRF.move(TEST_STEPS);
        stepperLF.move(TEST_STEPS);
        stepperRB.move(TEST_STEPS);
        stepperLB.move(TEST_STEPS);
    }
    
    void moveBackward() {
        stepperRF.move(-TEST_STEPS);
        stepperLF.move(-TEST_STEPS);
        stepperRB.move(-TEST_STEPS);
        stepperLB.move(-TEST_STEPS);
    }
    
    void moveRight() {  
        stepperRF.move(TEST_STEPS);
        stepperLF.move(-TEST_STEPS);
        stepperRB.move(-TEST_STEPS);
        stepperLB.move(TEST_STEPS);
    }
    
    void moveLeft() { 
        stepperRF.move(-TEST_STEPS);
        stepperLF.move(TEST_STEPS);
        stepperRB.move(TEST_STEPS);
        stepperLB.move(-TEST_STEPS);
    }
    
    void loop() {
        // -------------------- Serial Command --------------------
        if (Serial.available()) {
            String cmd = Serial.readStringUntil('\n');
            cmd.trim();
            cmd.toUpperCase();
            if (cmd == "START") {
                testMode = true;
                Serial.println("Test started");
            }
            if (cmd == "STOP") {
                testMode = false;
                Serial.println("Test stopped");
            }
        }
    
        // -------------------- Voltage & OLED (every 1 sec) --------------------
        if (millis() - lastVBATread >= 1000) {  // 1000 ms = 1 sec
            lastVBATread = millis();
    
            float v_bat = readVBAT();
            float v_sys = readVSYS();
    
            u8g2.clearBuffer();
            u8g2.setFont(u8g2_font_10x20_tr);
    
            char buf1[16]; snprintf(buf1, sizeof(buf1), "VBAT: %.2fV", v_bat);
            char buf2[16]; snprintf(buf2, sizeof(buf2), "VSYS: %.2fV", v_sys);
    
            u8g2.drawStr(0, 20, buf1);
            u8g2.drawStr(0, 40, buf2);
            u8g2.sendBuffer();
    
            // LED blink to indicate sampling
            digitalWrite(LED_BUILTIN, HIGH);
            delay(10);
            digitalWrite(LED_BUILTIN, LOW);
        }
    
        // -------------------- Test Mode Movement --------------------
        if (testMode && millis() - lastMoveTime > MOVE_INTERVAL) {
            lastMoveTime = millis();
    
            switch (movementStep) {
                case 0: moveForward();  break;
                case 1: moveRight();    break;
                case 2: moveBackward(); break;
                case 3: moveLeft();     break;
            }
    
            runAllSteppers();
    
            movementStep++;
            if (movementStep > 3)
            movementStep = 0;   // Loop back to forward
        }
    }    
    

    Python code (PySerial):

    • Connects to the Arduino’s serial port using pyserial
    • Reads incoming serial data (VBAT values printed by Arduino)
    • Parses and stores data in a CSV file with timestamp
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    import serial
    import csv
    from datetime import datetime
    
    # Serial 
    ser = serial.Serial('/dev/tty.usbmodem14101', 14101)
    
    # Make CSV file and writer
    csv_file = open("vbat_log.csv", "a", newline="")
    writer = csv.writer(csv_file)
    
    print("Logging started... (Ctrl+C to stop)")
    
    try:
        while True:
            line = ser.readline().decode("utf-8", errors="ignore").strip()
            print(line)
    
            # --- Save to CSV with timestamp ---
            timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            writer.writerow([timestamp, line])
    
    except KeyboardInterrupt:
        print("Logging stopped.")
    
    finally:
        csv_file.close()
        ser.close()
    

    Run the Python script (Terminal): python3 myscript.py

  4. Outcome:

    • 39 minutes of continuous operation test.
    • Battery voltage dropped from approximately 11.9 V to 11.6 V.

    Voltage graph:

    • Trendline: y = -9.44E-05 * x (x = Timestamp (sec), y = Vbat (V))
    • Voltage drop: 0.0000944 V / sec ≈ 0.34 V / h
    • Time until 12.6V drops to 10.8V: (12.6V - 10.8V) / 0.34V/h ≈ 5.29 h

    Estimated maximum continuous operating time: 5.29 h

    • Battery: 6000 mAh 12.6 V - 10.8 V
    • AccelStepper setting: MaxSpeed: 1500, Acceleration: 2000

4. Maximum current

  1. Overview:

    • Calculate the maximum current based on the I_limit set in the A4988 motor driver

      Current limit formula: I_limit = V_REF / (8 * R_SENSE)

      Examples:

      • V_REF setting: 0.5 V (Final project V0.1):

        I_limit = 0.5 V / (8 * 0.068Ω) ≈ 1.47 A
        I_total = 4 x 0.92 A ≈ 3.68 A
        
      • V_REF setting: 0.8 V:

        I_limit = 0.8 V / (8 * 0.068Ω) ≈ 0.92 A
        I_total = 4 x 0.92 A ≈ 5.88 A
        
    • Use a multimeter to measure the maximum current the machine consumes

  2. Wiring:

    • Connect a multimeter in series with the 12v line

    *This test was abandoned due to lack of parts for wiring the multimeter in series, and attempting it with jump wires and alligator clips seems unsafe.


Interface

  1. SPI: PMW3901 optical flow sensor
  2. I2C: OLED
  3. UART with XIAO ESP32, RP2040