Raspberry Pi Distance Moved

Hi Guys,
I have a little pi based rover and I’m looking to add a “distance moved” sensor. I’m thinking I could do this with some kind of Accelerometer but I don’t know much about them. My rover already has a distance to object measurement built in. I’d like to include a “north south” and “east west” measurement relative to a starting point.
Also a third axis to measure rotation around the same plane. If I start off pointing north for example I should be able to keep track of my current location and orientation.
The angle or slope isn’t really important but it might be fun to play with later on and adding extra features.
I have done a bit of searching but most things I find measure distance to object. Any suggestions on what hardware could be used to do this?
Once I know what hardware to use I may have more luck finding an example project to use as inspiration.

Thanks in advance


Hi David
One of the Core guys (Michael I think) had this little add on board with I think a Magnetometer which he had displaying like a magnetic compass. This was a demo video a short time back. You should find it if you can filter out all the Core videos. It is a PicoDev product SKU:CE07937 and might do your job.
Cheers Bob

1 Like

Core has a good video on using the Pi with a gyroscope and acceleromter module to measure acceleration in three axes…

It is possible to use that information to calculate position (an inertial navigation system) but it is difficult.
For example:
Research on distance measurement method based on micro-accelerometer: AIP Advances: Vol 11, No 5 (scitation.org)
is a comprehensive discussion of the whole problem that indicates the complexity of the maths. I can’t see that someone has actually done it and posted code, but you could try searching using terms from those references.


Hi David,

Other than Bob and Jeffs suggestions I’d have a look at:

  • Encoders - they’ll get the overall displacement but not an as the bird flys distance from a specific point
  • If you’re outdoors I’d have a look at a GPS
  • Getting much pricier a SLAMTEC 2D LIDAR module makes a 2D map of the room you’re in (with a 12m range)

Thanks for the reply Bob. I’ll check it out.
From another response the distance thing is looking more complicated than I first thought. At a minimum “you are now facing west” would be an interesting start.

Your right. That looks complicated :slight_smile:
I haven’t given up. I’ll post my python library when it’s finished :))
Thanks for the info that’s really helpful

I found this example here with a formular that calculates distance based on time and acceleration

I have ordered some pico-dev bits but unfortunately the main item is not in stock and requires 40+ days delivery. I’ll just have to wait it out.
If I get it to work I’ll post my proto type code here.

1 Like

Investigated the Magnetometers on a Zumo Cart and a Micro:Bit to get simple North, South, East, West direction. It was not very successful. The Zumo was affected by the placement of the chip on the board, never got reliable readings. Even the batteries affected the reading.

The Micro:Bit was a little better but still was not very reliable.
I would have liked something like the compass app on my phone, suspect the phone app uses more than just a Magnetometer.

The Zumo had a front infrared sensor looking down, supposed to follow a line. It did detect the line but the type of ink and width of the line was an issue.

Used encoders built into the motors that drove another cart. Proved to be the most successful way to determine distance travelled. Although both motors did not turn at the same rate, so the cart drifted to one side. Fixed it by using the encoder values to determine how far a motor had turned.

My conclusion was, determining the movement of a robot cart is much more complex than I thought.

Anyway … hope you have more success than me.


Hi Jim,
Interesting story. Thanks for all the detail.
I ordered some bits that won’t break the bank.
Will take 4+ weeks to arrive so I won’t have any results for a while. I did get a accelerometer that will be a challenge to interpret I reckon. I also bought a compass and noted in the description it said can also be used to detect local magnetic fields :slight_smile:
I think when performing an impossible task it’s important to give it to someone who doesn’t know it’s impossible:)
I’ll try and post some results here.
My initial goal is to build a web interface to control the car then publish that page on the net and see what happens. That bit is nearly finished.
I wonder if people will try and break it or hack the OS.
I’ll contain it to my office initially as it has a camera on the front. Hopefully people will just enjoy snooping around. It it goes well I’ll build a simulated Mars environment and set little tasks.
The funny part is it could be total anarchy as there is no login or sharing. Anyone can do anything all at the same time which will be an experiment in itself.


Links to a cart I built with web interface, camera, etc.
Kept the grandsons amused for a few hours, taking pics, driving it around.
Happy for you to use whatever may be of use and happy to provide more details, if needed.



Hi James,
Very interesting project. My “cart” came as a kit from Freenove. Just add your own Pi and batteries. My pi has been lying around for nearly 10 years so it was a bit of a challenge getting it to work with all the latest stuff.
I don’t have kids so I’ll have to get the general population to play with it.
I’m not sure the wife will handle the cam driving around on its own. :slight_smile:
I’m toying with the idea of building a Mars scene on a large sheet of plywood.
Recharging is going to be an issue. I’m wondering if I can come up with a docking station where the rover can park and contacts can hit a standard charger.
All things to figure out.
Thanks for your interest!


Hi David,

While @Liam is on the money with his suggestions for sensing the distance moved, if you exhaust other options, Pimoroni’s Optical flow sensor may be a non-invasive way to get that data. It only tracks movement in x and y (like a mouse), but if you mounted it off-axis on the robot, it could sense rotation and forward/backward movement.

Let us know how you go with your project :slight_smile:


Off topic…
That’s cool, I’d thought about hacking up a mouse (not a :mouse2:) to achieve that. Good to know there’s a purpose built part for it. Actually that’d be handy for one of those wall based ploters too.