8) ARDUINO OSMC CODE monowheel

See tab on left number 11 for how to wire things to an OSMC robot power controller.

I recently worked out how to control the OSMC with an Arduino, including fixing one of the timers in the Arduino 1280 I used so we now have FAST pulse width modulation which the OSMC works better with, not the rather slow "standard" pwm the Arduino uses by default.

I know from forums others have struggled a bit controlling an OSMC with an Arduino so here are the main extracts from my monowheel scooter sketch 2013 that are responsible for sending motor power instructions to the OSMC. 

NOTE: I used an Arduino Mega 1280 (the older cheaper Mega still available on ebay). Timer 2 affects pwm on pins 9 and 10 so you have to use 9 and 10 on the Arduino as the new , fast, pwm outputs, even though it has other pins labelled as pwm-capable.

//At the start of the sketch where we set everything up…………………………….


/*   Notes on the OSMC pin connections and how we might connect an Arduino to the OSMC




    ALI -> OSMC pin 6         must connect to a pwm output pin on the Arduino, more on this later

    BLI -> OSMC pin 8         must connect to a pwm output pin on the Arduino, more on this later


    Disable -> OSMC pin 4     connect to a normal digital out pin on Arduino

    BHI -> OSMC pin 7         connect to a normal digital out pin on Arduino

    AHI -> OSMC pin 5         connect to a normal digital out pin on Arduino


    Vbatt/10 -> OSMC pin 3    connect to an analog input of the Arduino, scale the measured value accordingly and you get the main battery voltage.







int ahiPin = 8; // pin 8 on arduino to AHI on OSMC which is OSMC pin 5

int bhiPin = 7; //pin 7 on arduino to BHI on OSMC which is OSMC pin 7


//********************************VERY IMPORTANT***********************

int aliPin = 10;   //NOTE I am using an Arduino Mega 1280 (the cheapo Mega available on ebay). Pins 9 and 10 are pwm capable and controlled by Timer 2

int bliPin = 9;    //This is important as we are going to mess with Timer 2 so the pwm frequency changes to a much faster rate than the standard pwm on Arduinos.

//We want this because it is so fast the motor "whine" becomes higher than human ear can hear and also it is much smoother in terms of speed control.

//So, Use pins 9 and 10 for this and not any other pwm pins.

int osmcdisablePin = 4; //this pin 4 on arduino disables osmc if it is set to HIGH (i.e. 5V sent to osmc pin 4)





//in void setup() we set our Arduino output pins AND we alter speed of Timer 2 (which controls pwm frequency on Pins 9 and 10 on the Arduino Mega 1280)……………………………..


  //digital outputs

  pinMode(ahiPin, OUTPUT);

  pinMode(bhiPin, OUTPUT);

  pinMode(osmcdisablePin, OUTPUT);


  pinMode(aliPin, OUTPUT);

  pinMode(bliPin, OUTPUT);




TCCR2B = TCCR2B & 0b11111000 | 0x01;

//Alters the speed of "Timer 2" so it goes much faster than default setting on the Arduino, this means any pwm pins controlled by Timer 2 go faster too, and they are Pins 9 and 10.

//sets Arduino 1280 Mega's pins 10 and 9 to frequency 31250 Phase-correct pwm (I think) it works OK anyway.

//Might be same for a Mega 2560 but I have not actually tried it to say for sure with a 2560.







//In our set_motor code we now send instructions to the OSMC……………………………….


void set_motor()   {




  level = level * 500; //changes it to a scale of about -250 to +250 which our Arduino pwm output code requires.

  if (level < -254) {level = -254;}  //stops stupid values being generated it cannot use (limit is 255).

  if (level > 254) {level = 254;}



//NB: If not pressing the deadman switch we want power to motor cut completely……………………..

digitalWrite(osmcdisablePin, HIGH); //5V to osmc pin 4 will DISABLE the OSMC




//How to make the OSMC respond to our “motortorque” value which is 0 for no power, -254 for full reverse and +254 for full forwards……………….


digitalWrite(osmcdisablePin, LOW); //0V to osmc pin 4 will ENABLE the OSMC

digitalWrite(ahiPin, HIGH);  //AHI and BHI have to be high on osmc for it to work

digitalWrite(bhiPin, HIGH);



 if (motortorque < 0){

      analogWrite(aliPin, -motortorque);  //pwm to OSMC pin 6  the -ve sign makes the ALI value back to a +ve integer value

      analogWrite(bliPin, 0); //0V output to OSMC pin 8


 else {

      analogWrite(aliPin, 0); //0V to OSMC pin 6

      analogWrite(bliPin, motortorque); //pwm to OSMC pin 8