I need help in creating a self balancing car (code)

2 days ago i have started designing a car with 2 wheels, i want it to balance alone and control it with my phone. Ive built phone controlled cars before but none of which only had 2 wheels.
I'm having trouble since i don't have the right components, instead of a gyroscope i have an accelerometer, GY-61, an a basic l298n motor driver and Arduino nano, Ive been trying to figure out a smart way to use the accelerometer and make it act as a gyroscope, Ive came to the conclusion of that i want to use the z axis and x axis because if the z value decreases and x increases or decreases then the car must have tilted in one direction o the other then the motors will turn in the opposite direction. unfortunately Ive been trying to figure out how to set it up in code but i simply cant and I'm now asking for the communities help.

An accelerometer isn't ideal for detecting angular velocity like a gyroscope in a moving vehicle (assuming that if you call that a car, then it's moving)

An accelerometer can measure tilt when the system is stationary or moving slowly. However, during faster movements, the accelerometer will also pick up linear accelerations, which can confuse the tilt measurements. This makes it harder to accurately determine the car's tilt solely based on accelerometer data.

You could attempt to combine accelerometer data with estimates of the car's motion to improve tilt detection in a fusion like algorithm but this is complex and I'm not even sure it will work.

For reliable and dynamic balancing, incorporating a gyroscope would be my recommendation.

1 Like
if (acc > threshold){motorCompensate(back);}
if (acc < -threshold){motorCompensate(forward);}

Seems like a tuning nightmare - another data point like lidar/sonar or just a tilt switch would be invaluable.

1 Like

Did you check any of the gazillon tutorials doing exactly that on google or youtube?

1 Like

exactly ? the ones I had seen often use a 6 DoF Module at minimum (like the MPU6050) — not just an accelerometer

Oh, there are. And they all suffer from oszillation problems due to not using gyro :slight_smile:

BTW ... https://forum.arduino.cc/t/balancing-robot-drifting-issue-on-different-surfaces-forward-backward-drift/

OK - so no need for the OP to spend time looking at that :wink:

1 Like

Well, depending on his setup it might even work - well, kind of. And who knows, maybe he's smarter than those legions before him ...

You realize the internet is being burned as we speak.
"Search for it" is less and less valuable advice. You are more likely to get a 15 year old answer regurgitated umpteen times by AI, that was wrong or irrelevant in the first place, than anything useful.

I'm aware of those facts, and since the car is controlled by the phone, if the car is moving linearly due a command sent by the phone then that motion will be disregarded

i have but non are using an accelerometer they are all using a MPU6050 chip
which i dont have

man i even tried ai its been a trouble shooting nightmare

the problem is the lack of gyro, im trying to get smth to work that incorporates 2 axis, the z and the x. the reasoning behind this is that if the x were to change then there some tilt (wanted linear movement will only be sent via wifi commands from the phone so it will be easy to distinguish) and if a change in z happens then the vertical platform has tilted will use the z to make sure its tilt and the x to know the direction

im doing this as a leaning challenge to help improve my programming skills unfortunately this time i ran out out of luck and chat gpt isnt helping

i dont care abt the oscillation problems tbh i jst want it work with the limited hardware i have and the coding experience is jst a bonus

ive tried such a simple approach but that didnt work
ps ive been in the Arduino world for around 2 years now but i like limiting myself with the components i use since i like the challenge but this is the first time ive faced e such troubles with code

Hopefully you don't read this until 7.5h from now, but I'm the winning bid at $0.30 on 4 different 6050 6DOFs on ebay right now. They do tend to be finnicky.

where i live its quite hard to get Arduino parts and sensors and buying online internationally will boost the price 10 times and customs will cost more than shipping itself if i need to get a part i need at least a week to source it either from physical shops or an online store that operates locally

So there is a new challenge:

How to get an MPU6050 on an unusal way.

Some people made the following experiment:
sending a letter by private people just travveling from europe to the USA. Just take it and transport it into the right direction regardless how far you come. Average inbetween stations was 6.

So where are you living? What companies have people that might travel to places where sensor-suppport is good?

How about somebody you know sending a simple small private package?
How about people that you know that travel ?
How about tourists?

It’s a big if. The moment your car transition from still to in motion, you have acceleration until you reach cruising speed, which you won’t get because the car will have flipped over due to the unbalance…

1 Like

i can get it within a week but i like the challenge and trouble shooting