This is a very simple application of a neural net solution for controlling a two wheeled self balancing robot. The robot does not do anything more than self balance; no control for directional control. But the project was simply to see if I could figure out how to use a neural net rather than a PID controller. Currently, I have a very primitive understanding of neural nets and I understand the basic concepts but I believe a lot more sophistication could be utilized in applying an ANN (Artificial Neural Network) to a self balancing robot. I hope there will be some comments by persons in the know about NNs with some ideas that could be applied here.
I adapted Sean Hodgins neural net code and you can find more specifics on how it works on his instructable here:…
I adapted midhun_s excellent code for balancing my robot:…
His code is some of the simplest and most easily implemented code that I have found for the Arduino for self balancing. However, I modified his code to get rid of the interrupts and instead just use a millis() timing routine so my code is even simpler.
I combined both of these excellent routines into a single program. It uses a neural net with a single input node, two hidden nodes and a single output node. The program first calls a training routine which takes only a few seconds so you can run it every time you start up the robot. Then the neural net routine runs and uses the standard sigmoid activation function which contains the node values to within 0 to 1. You simply need to map your input sensor values (which is just the tilt angle of the robot) to between 0 and 1, input the values into the neural net function which then returns an output in the value between 0 and 1. Then map that output value to a useful value for your motors, between -255 and 255 in this case.
A single input neural net is kind of overkill for this robot but as I explain later, more inputs could be used. This will give you a starting point for using neural nets on the Arduino.”