4

related: how to control a servo motor from a raspberry pi

The raspberry Pi normally outputs 3.3 V signals from it's pins, however there are logic level converters for interfacing with higher-voltage parts as well, and I am not sure if there was one in use in the linked question or not (yes, I asked it as a comment a while ago - no answer), thus this question

Philo has managed to DIY-control it, but I don't see the details from his site

P.S. If it wasn't clear, I am looking for the level at which the control signal is, not the 9v/7.4v power line runs at

3 Answers 3

2

The answer to the question you linked contains the answer you are looking for:

  • To move in one direction, send a PWM signal (1200 Hz, 0 to 100% duty cycle) on C1 and keep C2 at GND level. As duty cycle varies, servo motor will move along 7 positions on one side. See this video.
  • To move in the other direction and reach the 7 other positions, send PWM to C2 and keep C1 at ground level.

PWM means Pulse Width Modulation, so you need something capable of generating this signal. The voltage is the same as what you apply to the 9V terminal. Only the pulse width is varied. And GND means "ground" which is another way of saying 0V.

So, to do this with a Raspberry Pi or other microcontroller, you will need a transistor to convert the voltage. There is virtually no load on C1/C2 in this case, so any size of transistor will do the job without risk of overload. See this page for some example circuits.

2
  • Not seeing the source for the fact that the PWM signal is 9v, and having the signal voltage at the same level as the motor voltage would be something quite unusual - have you tested it with 9v and it worked? have you looked at the c1/c2 lines under an oscilloscope to verify the voltage levels from a standard lego controller? Sep 8, 2015 at 19:49
  • Yes, it is unusual and yes I have looked at it with an oscilloscope. On other power functions motors, C1 and C2 are actually used to drive the motor itself and the 9V and GND pins are not used at all. So, of course, C1 and C2 are full battery voltage since they power the motor. The servo motor just takes advantage of this existing mechanism as a means of transmitting a variable signal from the controller to the motor. Sep 9, 2015 at 0:50
2

The C1 and C2 lines are either connected to the 9V power supply or grounded because they directly drive classic PF motors and are not just used to carry commands. The 9V and GND poles also need to be properly connected because the PF servo will draw power from those.

To drive any LEGO Power Functions motor using a single-board computer (Pi / BeagleBone / Arduino), the easiest way is to use an H-bridge Integrated Circuit, which will safely separate the SBC (3.3V or 5V) from the high-power stuff (9V motors). There are readily-made circuit boards which are easy to interface with an SBC on e.g. dx.com or aliexpress.

Don't drive LEGO PF motors with the output of your SBC directly or you may very well fry it, because motors will draw too much current.

I successfully used this L298N-based circuit board to drive up to 4 PF motors at any speed in both directions with a Beaglebone Black. It can accept 3.3V commands directly but the PWM output from the SBC better be level-shifted to 5V so that the duty cycle is really respected. You can use something like this to shift the voltage. The board also features a 5V power source so you can power the SBC from it using only LEGO battery boxes.

This H-brige module accepts 3 inputs per motor:

  • forward
  • barckward
  • enable

forward/backward inputs of the control module can be tied to ON/OFF outputs of the GPIO and the enable input to a PWM output of the GPIO. The duty cycle of the PWM can then be used to control the speed of a classic PF motor, or the angle of a PF servo motor, while forward/backward will control the direction (enabling both will result in braking the motor).

Note that you'll have to be carefull with the outputs of the RPi as those are open-collector outputs, so don't fry your SBC. Arduino and Beaglebone are easier to use.

4
  • Thanks for the clarification - I have looked at the level converter boards, but I didn't see any that go all the way up to 9v, 5v or 5.5v seems to be the common stopping point, and I imagine that the servo would still work with mostly drained li-ion batteries (2s ~ 6v or so) - any idea what the lower bound of the input voltage is like? Sep 10, 2015 at 20:04
  • Level converters are for converting logic levels, not for converting low-current commands to drive a load. That's what H-bridges are for. So the circuit you are looking for is an H-bridge. The L298N I used expects 5V TTL inputs, but the input is considered high from 2.5V on so they can be driven with 3.3V outputs and this normally wouldn't need level converters. However I found that the PWM ouputs of the Beaglebone were not clean, so the amount of time the signal is above 2.5V does not match the required duty cycle, hence the level converters to "clean" the signal.
    – Shadocko
    Sep 11, 2015 at 6:53
  • so I use an H bridge to convert the PWM signal to 9v for the C1/C2 lines for a servo motor? Sep 11, 2015 at 17:58
  • 1
    Yes, you should use an H-bridge so that you don't have to connect the mass of your RPi to the 9V mass, thus avoiding any current surge that could damage your SBC (loads are active components!). The output circuit using a FET in the page linked-to by David Lechner would also work but you would need a more complicated setup to rotate the servo in both directions. An H-bridge (which is just four MOSFETs and diodes) plus logic gates, like what the L298N provides in an IC package, is easier and requires less PWM outputs (the RPi's don't have many). The LEGO IR receiver V2 uses a DRV8833 from TI.
    – Shadocko
    Sep 14, 2015 at 9:59
0

You can make a level converter that will safely mediate between 9v and 3.3 or 5v using a couple of resistors and a FET - the operation is a bit subtle, as it depends on a peculiar characteristic of typical FETs, but it does work. See sketch - you can buy these as ready made units with four or more level shifters per tiny board, for very little money, from the same kind of suppliers who sell arduinos etc. This design has the added advantage of being bi-directional in a way that works with I2C, too. enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.