DIY racing pedals

Hi! I began this project some days ago and today I've been messing with a loadcell and hx711 module. I've done a soldering trick for it to read at 80hz instead of the usual 10hz, and it seems like it worked, but I'm not sure...


I don't know if with this alone I can be sure that it's reading at 80hz or if it's just noise, but it seems like it's working! And in the case that it is: does someone know why some times it reads "hx711 not found"? Is it because the delay and the module reading frequency are out of sync? Is there a way to fix that? And lastly: how do I turn this returned value into an axis reading, for the joystick library? Like this: let's suppose that "617000" should be 0% of the brake axis on the joystick code, and "300000" should be 100%. How do I do that?

Where is that delay? Can't see it.
Reading 3 consistent digits is good I suppose.
There's a lot You missed to tell.
You're supposed to have been reading this link before posting: How to get the best out of this forum - Using Arduino / Project Guidance - Arduino Forum
Please update Your post.

Sorry!
Hardware: the sketch is running on an Nano (168p), but I plan on implementing it on an Leonardo

The code is the basic hx711 library, with just the delay set to 13 instead of 1000 (80hz is equivalent of 12.5 ms, I rounded it up to 13 and used the same pins):

#include "HX711.h"

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;

HX711 scale;

void setup() {
  Serial.begin(57600);
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
}

void loop() {

  if (scale.is_ready()) {
    long reading = scale.read();
    Serial.print("HX711 reading: ");
    Serial.println(reading);
  } else {
    Serial.println("HX711 not found.");
  }

  delay(13);
  
}

Me to. You just keep Your boots moving on.

Why can't You get the first question solved? Why add another question before the first matter is settled?

Why not use 115200 as baudrate? It works fine using reasonably long USB cables. That reduces the delay caused by the code execution.
To create frequency use millis(), or microSeconds() as in the topic "Blink without delay".

What does the baudrate do? I just copied the code, I don't know why it uses 57600 and not 115200... But I'll try it! I'll read the "blink without delay" topic too, and see if I can implement it. I'll update here once I'm done!

Your short Serial.print will likely not make any delay but long or many Serial.print will fill the output buffer and make the serial.print call hang/wait for output buffer space.
Use millis/microseconds to stamp the time and then do the activity when time has reached the next transmission time.

millis() - previousMillis > interval.... then do .....

See if this fixes it:

#include "HX711.h"

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;

HX711 scale;

void setup() {
  Serial.begin(57600);
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
}

void loop() {
  if (scale.is_ready()) {
    long reading = scale.read();
    Serial.print("HX711 reading: ");
    Serial.println(reading);
    delay(13);
  }
}

sorry for not giving update! didn't have much time last days to tinker with the project
the baudrate worked perfectly! it's reading dozens of cycles per second now! but I couldn't implement the millis yet because I didn't quite get it... I'm struggling quite a bit because I have 0 experience with arduino IDE and I'm completely lost lol
but I was thinking about it and it seems that I'll surely have to replace the delay, because if I got it right, everytime the code enters the 12500 microseconds delay, it will stop reading the hall sensors responsible for the accelerator and clutch. but I don't know how to do it...

Look at the example code "Blink without delay"! It's not har.
You register the time (millis) when a reading is done. Before entering the read code check that the current time ( millis() ) - lastRead > interval.

Why use "delay" at all, whether delay(13) or via the millis() method?

The purpose of the

if(scale.is_ready())

is to allow the code to do other things while the HX711 is thinking, and then read the HX711 when it is ready.

Since even 80 Hz is glacially slow, loop() will "loop" many, many times in between HX711 reads.

Edit to add: sorry, didn't mean this to be a to "reply to" @JCA34F It was (mostly) directed at the OP...

the original example sketch had not only the delay function, but it was a whole second (1000)! I was afraid that taking off the delay my board would just blow up, I don't really know how these things work yet :sweat_smile:

A Pro Micro would be better. Leonardo is the same shape as an Uno, so not so easy to build into a project.

I suggest starting with the code @JCA34F posted, but without the delay(13).

I already bought one... I hate soldering, and I plan on making the pedals bulky and with a lot of space, so I think it will be alright. I have 0 experience though, it's my first project

about the code... alright, I'll try it! I'll run it on my nano so if it catches fire, I'll minimize the losses :laughing:

Read about "map()" here: Arduino Reference - Arduino Reference

And while you're there, spend some time reading about the other available features.

everything worked! with the higher baudrate and without the delay function, it is reading lightning fast compared to the basic sketch. the code got a lot simpler, too:

#include "HX711.h"

const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;

HX711 scale;

void setup() {
  Serial.begin(115200);
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
}

void loop() {

  if (scale.is_ready()) {
    long reading = scale.read();
    Serial.println(reading);
  }
}

about the features... well, I've read a lot. the problem is that I don't understand most of what I read... I have no background in programming and it is my first time using arduino IDE. I'm finding most things very challenging. I also didn't find any sistematically organized begginner material, so I'm pretty lost with... everything lol
but hey, time for the real challenge now... to implement this reading into the joystick code. time to suffer :laughing:

Try this:

oh, that seems cool! I'll check it. thanks!
also, just to mention... well, I wasn't expecting it, but I ended up finishing the entire code by myself yesterday still! I already adapted the joystick library to read the hx711 value and interpret it as the brake. I tried it on assetto corsa and it worked fine, the refresh rate in the game is very responsive (although it has atrocious latency on the menu/content manager, wich is kinda bizarre but changes nothing lol). I'm gonna post it here for now just to thank you all, but later in this month I'll create a new topic with the code to both serve as help for people with similar projects in mind, and for people to help with some adjustments. for now, I'll focus on the "physical part", wich I guess will be the hardest :sweat_smile:

#include "HX711.h"
#include <Joystick.h>
#define joyX A1
#define joyThrottle A0
#define joyBrake 2

Joystick_ Joystick(0x15, JOYSTICK_TYPE_JOYSTICK, 0, 0, true, false, false, false, false, false, false, true, false, true, false);

const bool initAutoSendState = true;

const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;

HX711 scale;

int xAxis_ = 0;
int throttle_ = 0;
int brake_ = 0;

void setup() {
  Serial.begin(115200);
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  Joystick.begin();
}

void loop() {
  
  xAxis_ = analogRead(joyX);
  xAxis_ = map(xAxis_,0,1023,0,255);
  Joystick.setXAxis(xAxis_);
  
  throttle_ = analogRead(joyThrottle);
  throttle_ = map(throttle_, 0, 1023, 0, 255);
  Joystick.setThrottle(throttle_);

if (scale.is_ready()) {
    long reading = scale.read();
    reading= map(reading, 615000, 900000, 0, 255);
    Serial.println(reading);
    brake_ = reading;
  }
  
  Joystick.setBrake(brake_);

delay(10);
}

just some details about it: 99% of this code was copied from the joystick library beginner's guide on youtube, the only changes I did was to include the hx711 library code wich you guys helped me with, and the map function for the brake_ variable to "understand" the serial input coming from the digital pin 2 (which took me almost 2 hours haha). on this case, the delay function was used for the code not to flood the usb bus, according to the guide. oh, and lastly, I put an empty X axis just to see if windows would recognize the gas pedal as a progress bar (I don't know how to call it...) instead of an axis.

Thank you for the follow-up. Good luck with your project.

PS: double definition for pin 2; probably don't need the first one...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.