Category Archives: hw config

myR: How to control DC Motor -part 2

So, let’s first complete the wiring.

Each motor needs 2 signals, one for the clockwise movement, one for the counterclockwise movement.

I’m using the  GPIO 18 ,23,24,25  connected directly to the 4 inputs of the bridge. See the diagram below.


Consider Motor 1. When GPIO 18 is high , the bridge set the Out1  also High and Out2 is the ground. And the motor turns cw. When GPIO23 is High the bridge inverts the outputs, so Out1  become the ground and Out2 is High and the motor turns ccw.

Finally , as we see in preview post, modulating the pulse width of the signal we can control the speed of the motors.

Beta1test. Bugs and a steps ahead

Yesterday I got this result:


Thanks to the following actions:

1)The test with the ball on the bottom of the drone,did not convince me. the friction on the ball, the  rotation limits due to the basement, were all components that are not present during the flight.

So I decided to switch to the more used and recognized  solution with the quadcopter tied up with 2 wires.


2) I realized  that something was wrong on the motors behaviour.When I  run the   the motor sound  was unstable and unconstant evenif  I was not  controlling it with PID ,just make them run in throttle speed.

This is not happening if I run the

Finally I was able to reproduce the problem and I realized there is a bug (or better an unexpected behaviour)  in the  library RPIO. If I run this loop (that reproduces the main loop of the beta1) I got the unstable motor rotation:

while true:

mymotor.servo.set_servo(, PW)



My explanation is everytime  calling the set_servo()  function, it resets to zero the signal than set it to the desired value PW).

Instead of that funciont I introduced the lower level function


When I put it in the test loop I got a perfect, stable, constant sound for the motor rotation.

This 2 improvements gave me the opportunity to move  a  great step haed. Now the tuning can be faster and more consistent.

Above just a graph showing the current behavior.In blue the requested   roll , in red the real roll when  the quadcopter is  using the pid controller. I still need to  tune it to reduce the time to reach the target (5/6 seconds)  and to avioid the oscillation ( +/- 3 degrees).But I’m really satisfy about the current situation.

Beta1 test preparation

This post introduces you the next test session.

In this phase I’m  developing the software to manage the 4 motors and the IMU.

I made the following modification on the frame:



I added an half sphere attached in the bottom, in order to create an unstable condition on the drone.I double check that  the blades can not touch the field with any orientation.

The target is to see the quadcopter to stay horizontal adjusting  the orientation (roll and pitch) by itsself.

Let’s try!

Preparing test session

It’ is now time to move from my desk to the garage to start the second phase of the development.

The targets are:

  • verify the start up procedure of the esc. I want to see if and how  I can start up properly the esc evenif it is already power on. This is useful in case I want to power rpi directly from  one Esc BEC.
  • verify the disturbance in the IMU data due to the motors vibration.Setup in the best way the filter severity
  • verify the control loop and tune the pid parameters for quadcopter rotation
  • develop the user remote control via wifi to control motors and quadcopter position

To do this tests I build up a frame that I hope it can allow me to stay safe.Here the picture of the original design and the final result.




I’ve  also finalized the wiring of the system including the IMU and an home-made board for the motors connections.rpi power is still via usb and ethernet connection via wire.


So the new motor connection setup is:

  • GPIO 18 (pin 12) >M0
  • GPIO 23 (pin 16) >M1
  • GPIO 24 (pin 18) >M2
  • GPIO 25 (pin 22) >M3




Tutorial: How to read data from IMU

In the previous post I described how to setup raspberry pi for connection with the IMU.

Now it is time to see how to read some data from the sensor.

First yuo need sw that manage the i2c interface.there are many examples.You can find one called on github.

Then it is necessary to have the code specific for the sensor,in my case a MPU6050.

I tryied for some days to build my own code, but I encountered problems related to unconsistent results: even if I did not move the sensor, the returned results were always different. I suspect it was a problem on how I formatted the values.

Finally, Thanks to the great job done by Hove in  his blog, I used his code and I’m now able to collect correct data from the sensor.

I did some minor modification and prepare this IMU_test files.

So I started some preliminary tests to verify which is the sensor behaviour.

I fixed the sensor on a bar ,horizontally, than turned the bar by a known angle ( 13 degrees,measured with my smartphone level) then move back to horizontal.


I recorded on a file the sensor data : acceleration along axis (from ACCelerometer) and rotational speed (from GYRO scope). On excel sheet  I calculated the angle around x  respect the ACC and respect the GYRO:

  • rx ACC=DEGREES(ATAN2(accZ+9.8;accY))
  • rx(i) GYRO=wx(i) *dt+wx(i-1)

Below you can see the graph.


I underline in the picture the 2 tipical problems on the IMU :

  • the Gyro drift (you can see an angle of 1  degree while it reality it was 0)
  • the Accelerometer sensibility to noise.

So next  development step is to filter/reduce this 2 problems by combining the 2 sensors results.

IMU Sensor Installation

Since a week ago, I ‘ve been looking into my new IMU: Inertial Measurement Unit.

I bought  from e-bay a MPU6050, a sensor including:

  • a gyroscope that can return the angular speed around the 3  axis x,y,z.
  • an accelerometer  that can return the linear acceleration along the 3  axis x,y,z.

This sensor can communicate trou I2C interface.

Below the electrical drawing:









For the sw installation I fuond a clear tutorial on the Adafruit website ,so I just report here a memo for the necessary steps:

in sudo nano /etc/modules add:

  • i2c-bcm2708
  • i2c-dev

sudo apt-get install python-smbus

sudo apt-get install i2c-tools

in sudo nano /etc/modprobe.d/raspi-blacklist.conf comment:

  • #blacklist spi-bcm2708
  • #blacklist i2c-bcm2708

Now ,running the command sudo i2cdetect -y 1  yuo have to see the address 0x68, corresponding to the default sensor address.

Tutorial:How to control a brushless motor with raspberry pi

UPDATED 2014.06.15 Important Note: I discovered that the usage of the function pwm.setservo() it is not adeguate to control the motor in a loop. Everytime it is called, it sets the pwm to zero than it sets the new value. the result is a loss of speed (and so power ) of the motor. I just start to use the pwm.add_channel_pulse() instead and it solve the problem.See for the last version of  code.


Last Saturday it was the great day: first brushless moved! And moved with rpi!!!

Right now I prefer not to mix up too much things, so the rpi is powered via usb and the connection is done by ethernet cable.


Those the details for the necessary steps.

  1. Mount the motor firmly. IMPORTANT: Remember it will move,so fix it well!
  2. Connect the motor wires to the motor has got 3 cables:red,black and yellow.I decided always to connect the black on the middle, and i can invert yellow and red to change rotation side.
  3. Do NOT connect yet the esc power cable to the battery
  4. Connect esc 0v signal (black) to th rpi ground (for example pin 25)
  5. Connent PWM signal(white) on a rpi output.In my example i connect on pin 11 called GPIO17. This value(17) is used in the program.
  6. Connect Ethernet cable
  7. Connect USB to power on rpimotor_test_setup

Let’s see now the software. It is necessary to install on rpi the RPIO library.You an find how to do it in the SW installation post.

  1. Donwload the motor_test folder tha includes and and copy in  home/python/motor_test on your pc.
  2. On the pc terminal copy the folder on rpi: (it is requred the rpi password):
    scp -r python/motor_test  pi@
    password : raspberry
  3. On the pc terminal run the command to connect to the terminal is the rpi terminal.
    ssh pi@
    password : raspberry
  4. Finally run the motor_test program.Important: the startu sequence of an ESC can vary by the model.See the ESC  page for details.
    cd python/motor_test 
    sudo python
  5. Follow the program instruction.


After the first ENTER , the PWM signal is set to the maximun value (a pulse width= 2000us every 20ms).Now you can connect the ESC power cables.If everything goes right (correct voltage, cell number correct) ESC is ready to pilot the motor.PWM signal goes to 1000us,the minimum value.So it is possible to play with the motor speed.My motor starts to move over 5%,it means 1050us.This value depends from motor inertia from friction and from battery charge level.

HW Dev: Frame

This week I was concentrated on the frame of the quadcopter.

hw1I found in a bricolage center this specific cross for mounting aluminium bars togheter.It was available only the format 20x20mm. So I bought it togheter with one meter of alu bar.

hw3I cutted 4 pieces by 250 mm and I mounted in a second.

I drilled the 4 arms  for the mounting of the motors and for fixing the cross.


The motor fixture is drilled for a M3 screw, so I adopted for all the frame the same screws: 12 screws M3 of 30mm.


Note: To be sure that I can remount the arms always on the same position I mark the cross and the arms with a cutter.


  • Bar: 5.90 EUR
  • Cross: 5.40 EUR
  • Screws: 0.8 EUR (25pz)