Built-in project · SparkFun Pro Micro

Load cell pedals on a Pro Micro

A load cell brake on an HX711 that responds to force, a hall sensor throttle and a pot clutch, each calibrated and sent as one Joystick.h report. The whole build, a SparkFun Pro Micro and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Advanced About 35 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $19.80. On Mokxi they're free.

Load cell pedals on a Pro Micro · SparkFun Pro Microlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Measuring force with a load cell and an HX711
  • Taring a sensor so rest reads as zero
  • Scaling a raw reading between a rest and a floor value
  • Adding a deadzone and a progressive curve to a pedal
  • Sending pedals to a PC as a USB game controller
  • Difficulty: Advanced
  • Time: About 35 minutes
  • Board: SparkFun Pro Micro
  • Parts: 5

How it works

Three pedals feed one USB game controller. The brake is a load cell read by an HX711 chip (DT on pin 4, SCK on pin 3). It senses how hard you push, not how far. The throttle is a hall sensor on A1, and the clutch is a potentiometer on A0.

At start-up the sketch reads the brake with no foot on it and treats that as zero. After that it turns each reading into kilograms, then into an axis value from 0 to 1023, and sends it as a joystick report.

A report goes out only when an axis has moved by more than a few counts. That stops noise in the last digits from flooding the PC. Mokxi shows each report as a [Joystick] line instead of sending it over real USB.

Parts list

5 parts, plus the jumper wires. They retail for about $19.80; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Load cell with HX711 pin GNDSparkFun Pro Micro pin GND
Load cell with HX711 pin SCKSparkFun Pro Micro pin 3
Load cell with HX711 pin DTSparkFun Pro Micro pin 4
Hall sensor, 49E linear pin GNDSparkFun Pro Micro pin GND
Potentiometer pin 1SparkFun Pro Micro pin GND
Load cell with HX711 pin VCCSparkFun Pro Micro pin VCC
Hall sensor, 49E linear pin VCCSparkFun Pro Micro pin VCC
Potentiometer pin 3SparkFun Pro Micro pin VCC
Hall sensor, 49E linear pin OUTSparkFun Pro Micro pin A1
Potentiometer pin 2SparkFun Pro Micro pin A0

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · SparkFun Pro Micro
// Sim racing pedals on a Pro Micro: a load cell brake on an HX711, a hall
// sensor throttle and a potentiometer clutch, sent as one USB game controller
// report through the Arduino Joystick library's API (Joystick.h).
//
// Why a load cell for the brake. A pot or hall sensor measures how far a
// pedal has moved. A load cell measures how hard it is pushed, which is how a
// real brake pedal works: your leg remembers force far better than distance.
// That is why so many DIY pedal sets put a beam load cell under the brake and
// keep position sensors for the throttle and clutch.
//
// Wiring.
//
//   brake     HX711 DT on pin 4, SCK on pin 3, VCC 5 V, GND
//   throttle  SS49E hall sensor on A1 (a magnet on the pedal arm)
//   clutch    10k pot, wiper on A0
//
// In Mokxi: drag the HX711's load for the brake (in kilograms on the cell),
// slide the magnet towards the hall sensor for the throttle, and turn the
// knob for the clutch.
//
// The brake's numbers.
//   The HX711 is a 24-bit converter made for load cells. At start-up the
//   sketch reads it with no foot on the pedal and calls that zero (tare).
//   COUNTS_PER_KG turns counts into kilograms. Find it on your own cell: hang
//   or stand a known weight on it, take the reading minus the tare, and divide
//   by the kilograms. BRAKE_MAX_KG is the push that means full brake; a
//   higher number makes a stiffer, more progressive pedal. The HX711 runs at
//   80 readings a second here (its RATE pin high), not the 10 it starts at.
//
// Not simulated: the USB connection to a PC. Mokxi models this chip's USB as
// a serial pipe, so each report prints as a [Joystick] line. Flash the same
// sketch to a real Pro Micro and a racing game sees three pedal axes.

#include <HX711.h>
#include <Joystick.h>

// ---------------------------------------------------------------------------
// Make it yours
// ---------------------------------------------------------------------------

const uint8_t BRAKE_DT_PIN = 4;
const uint8_t BRAKE_SCK_PIN = 3;
const uint8_t THROTTLE_PIN = A1;
const uint8_t CLUTCH_PIN = A0;

// Counts per kilogram for this cell: a 100 kg, 1 mV/V beam at gain 128.
const long COUNTS_PER_KG = 21475;
// The push, in tenths of a kilogram, that is full brake (400 = 40 kg).
const long BRAKE_MAX_TENTHS = 400;
// Pushes under this count as no brake, so a resting foot does not drag it.
const long BRAKE_DEADZONE_TENTHS = 10;
// 0 is a straight line; 100 makes the first half of the travel gentle and
// the end sharp, the progressive feel many drivers like for trail braking.
const int BRAKE_CURVE = 50;

// The throttle's hall sensor reading at rest and floored.
const int THROTTLE_REST_RAW = 530;
const int THROTTLE_FLOOR_RAW = 820;
// The clutch pot's reading at rest and pushed in.
const int CLUTCH_REST_RAW = 0;
const int CLUTCH_FLOOR_RAW = 1023;
// A little dead travel at each end of the throttle and clutch, in percent.
const int END_DEADZONE = 3;

// Print the brake in kilograms each time it changes by half a kilogram.
const bool SHOW_KG = true;

// ---------------------------------------------------------------------------

HX711 brakeCell;
long tare = 0;

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_MULTI_AXIS, 0, 0,
                   false, false, true, false, false, false,
                   false, false, true, true, false);

int lastBrake = -10, lastThrottle = -10, lastClutch = -10;
long lastShownTenths = -100;

// A reading between rest and floor as 0..1023, with dead travel at each end.
int travel(int raw, int rest, int floorRaw) {
  long span = (long)floorRaw - rest;
  long at = (long)raw - rest;
  if (span < 0) {
    span = -span;
    at = -at;
  }
  long dead = span * END_DEADZONE / 100;
  long out = (at - dead) * 1023 / (span - 2 * dead);
  if (out < 0) out = 0;
  if (out > 1023) out = 1023;
  return (int)out;
}

// Tenths of a kilogram on the cell as 0..1023, with the deadzone and curve.
int brakeAxis(long tenths) {
  if (tenths <= BRAKE_DEADZONE_TENTHS) return 0;
  long x = (tenths - BRAKE_DEADZONE_TENTHS) * 1023 / (BRAKE_MAX_TENTHS - BRAKE_DEADZONE_TENTHS);
  if (x > 1023) x = 1023;
  long square = x * x / 1023;
  return (int)((x * (100 - BRAKE_CURVE) + square * BRAKE_CURVE) / 100);
}

// Report an axis when it has moved more than a few counts, or reached an end,
// so the load cell's last-digit noise does not send a report every reading.
bool moved(int now, int &last) {
  if (now > last + 6 || now < last - 6 || ((now == 0 || now == 1023) && now != last)) {
    last = now;
    return true;
  }
  return false;
}

void setup() {
  Serial.begin(115200);
  brakeCell.begin(BRAKE_DT_PIN, BRAKE_SCK_PIN);
  tare = brakeCell.read_average(8);  // no foot on the pedal yet
  Joystick.setZAxisRange(0, 1023);
  Joystick.setAcceleratorRange(0, 1023);
  Joystick.setBrakeRange(0, 1023);
  Joystick.begin(false);
  Serial.println("Pedals ready: brake tared.");
}

void loop() {
  bool changed = false;

  if (brakeCell.is_ready()) {
    long counts = brakeCell.read() - tare;
    long tenths = counts * 10 / COUNTS_PER_KG;
    if (tenths < 0) tenths = 0;
    changed |= moved(brakeAxis(tenths), lastBrake);
    if (SHOW_KG && (tenths > lastShownTenths + 5 || tenths < lastShownTenths - 5)) {
      lastShownTenths = tenths;
      Serial.print("Brake ");
      Serial.print(tenths / 10);
      Serial.print('.');
      Serial.print(tenths % 10);
      Serial.println(" kg");
    }
  }

  changed |= moved(travel(analogRead(THROTTLE_PIN), THROTTLE_REST_RAW, THROTTLE_FLOOR_RAW), lastThrottle);
  changed |= moved(travel(analogRead(CLUTCH_PIN), CLUTCH_REST_RAW, CLUTCH_FLOOR_RAW), lastClutch);

  if (changed) {
    Joystick.setAccelerator(lastThrottle);
    Joystick.setBrake(lastBrake < 0 ? 0 : lastBrake);
    Joystick.setZAxis(lastClutch);
    Joystick.sendState();
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

brakeAxis sketch.ino

This turns a push in tenths of a kilogram into an axis from 0 to 1023. A push under BRAKE_DEADZONE_TENTHS counts as zero, so a resting foot does not drag the brake. A push of BRAKE_MAX_TENTHS is full brake. BRAKE_CURVE then blends a straight line with a squared curve for a progressive feel.

// Tenths of a kilogram on the cell as 0..1023, with the deadzone and curve.
int brakeAxis(long tenths) {
  if (tenths <= BRAKE_DEADZONE_TENTHS) return 0;
  long x = (tenths - BRAKE_DEADZONE_TENTHS) * 1023 / (BRAKE_MAX_TENTHS - BRAKE_DEADZONE_TENTHS);
  if (x > 1023) x = 1023;
  long square = x * x / 1023;
  return (int)((x * (100 - BRAKE_CURVE) + square * BRAKE_CURVE) / 100);
}

travel sketch.ino

This scales a raw reading between a rest value and a floored value onto 0 to 1023. It leaves a small dead zone at each end (END_DEADZONE percent), and it works whether the floor value is higher or lower than rest. Both the throttle and the clutch use it.

// A reading between rest and floor as 0..1023, with dead travel at each end.
int travel(int raw, int rest, int floorRaw) {
  long span = (long)floorRaw - rest;
  long at = (long)raw - rest;
  if (span < 0) {
    span = -span;
    at = -at;
  }
  long dead = span * END_DEADZONE / 100;
  long out = (at - dead) * 1023 / (span - 2 * dead);
  if (out < 0) out = 0;
  if (out > 1023) out = 1023;
  return (int)out;
}

moved sketch.ino

This decides whether an axis is worth reporting. It returns true when the value has changed by more than 6 counts from the last one, or has just reached 0 or 1023. That keeps the load cell's jitter from sending a report on every reading.

// Report an axis when it has moved more than a few counts, or reached an end,
// so the load cell's last-digit noise does not send a report every reading.
bool moved(int now, int &last) {
  if (now > last + 6 || now < last - 6 || ((now == 0 || now == 1023) && now != last)) {
    last = now;
    return true;
  }
  return false;
}

loop sketch.ino

Each pass reads the brake when the HX711 is ready, subtracts tare, and converts counts to tenths of a kilogram. It also reads the throttle and clutch. If any axis moved, it sets all three on the Joystick and calls sendState.

void loop() {
  bool changed = false;

  if (brakeCell.is_ready()) {
    long counts = brakeCell.read() - tare;
    long tenths = counts * 10 / COUNTS_PER_KG;
    if (tenths < 0) tenths = 0;
    changed |= moved(brakeAxis(tenths), lastBrake);
    if (SHOW_KG && (tenths > lastShownTenths + 5 || tenths < lastShownTenths - 5)) {
      lastShownTenths = tenths;
      Serial.print("Brake ");
      Serial.print(tenths / 10);
      Serial.print('.');
      Serial.print(tenths % 10);
      Serial.println(" kg");
    }
  }

  changed |= moved(travel(analogRead(THROTTLE_PIN), THROTTLE_REST_RAW, THROTTLE_FLOOR_RAW), lastThrottle);
  changed |= moved(travel(analogRead(CLUTCH_PIN), CLUTCH_REST_RAW, CLUTCH_FLOOR_RAW), lastClutch);

  if (changed) {
    Joystick.setAccelerator(lastThrottle);
    Joystick.setBrake(lastBrake < 0 ? 0 : lastBrake);
    Joystick.setZAxis(lastClutch);
    Joystick.sendState();
  }
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The brake shows weight with nothing on it This project
    The sketch tares once in setup. Keep your foot off the pedal while it starts, then press Run again if the zero was wrong.
  • The brake feels too soft or too stiff This project
    Change BRAKE_MAX_TENTHS, the push that counts as full brake. A higher number makes a stiffer pedal. COUNTS_PER_KG must also match your own cell.
  • The throttle never reaches full This project
    Compare the hall sensor's real readings at rest and floored with THROTTLE_REST_RAW and THROTTLE_FLOOR_RAW, and update them.
  • The reading is zero, noisy, or drifts. Load cell with HX711
    Check wiring: E+ and E- to the excitation, A+ and A- to the signal wires. Swapping A+ and A- makes weight read negative. Keep wires short, and let it settle before taring.
  • The grams value is wrong. Load cell with HX711
    Call tare with nothing on the scale, then calibrate with a known weight to get the scale factor. Each load cell needs its own factor.
  • The reading never changes or sits at a strange value with no magnet. Hall sensor
    With no field the output rests near half the supply, about 2.5 V at 5 V. Read the change from that rest value, not from zero. Check pin order: VCC, GND, output.
  • The output is weak or looks wrong when a magnet is close. Hall sensor
    Pole matters. A south pole facing the marked side raises the output and a north pole lowers it. The part saturates near the rails, so keep the magnet at a distance that stays within range.
  • The reading jumps around or only covers part of the range. Potentiometer
    Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.