Windows Phone controlled car with Netduino and Bluetooth

Tweet about this on TwitterShare on FacebookShare on RedditShare on Google+Email this to someonePin on Pinterest

The houses in Durban, South Africa (where I live) usually have some form of gap underneath them that ranges from 30cm to big enough to crawl inside. As far as I know it is for ventilation to help with the heat. The problem is that a bunch of squeaky rodents have decided that mine is a good home, and unfortunately the gap is pretty small so I can’t fit. So I decided to make a little car that could go scout out the area and show me what was going on under there, and possibly how to stop it.

So I built a little Bluetooth controlled car using a Netduino and a Windows Phone as a remote.

My puppy seems to be a fan.

The idea is pretty simple: Make a car that I can control remotely that is small enough to fit under the house. And have a place to mount a second Windows Phone running Skype video so I can view live-video.

car1

Design

For the frame and wheels I designed models and 3D printed them.

car3

The wheels needed to get quite a lot of grip because the surface is very sandy, so I made them spiked so they could dig in nicely. The lip is sized to fit over the motor shafts. The back wheels were pretty similar except just had a dowel-sized hole to use as an axle.
image

 

The body is a bit more complicated. It needs to hold both motors (front-wheel driven), the Netduino, the Motor driver, and battery (9V).

To save space I printed a platform for the motor driver that allowed me to mount it on-top of the Netduino using the same holes. Look at 3 the vertical lines going through the holes – those are bolts that go through the base, Netduino, and motor driver platform. The actual motor driver then sits on top of the platform using the other 3 holes.

image

The 9v battery sits right at the back with an elastic band hooked around the triangle. Directly underneath that is where the wooden dowel is placed for the back wheels.

The motors sit between the pillars on either side with bolts coming in from both sides (could have them go right through but I didn’t have any long enough).

Hardware

Netduino (any model).

5v LED board. This isn’t required and was just for shining forwards. (eBay for $3)

2 x geared motors. (DX for $3 each)

Dual DC motor driver. (eBay for $ 5)

Serial Bluetooth module. (DX for $7)
9V battery
A frame (I 3D printed mine, but a block of wood would work too).

OK, now to wire it all up!
The Bluetooth module is powered from the Netduino 3V3 and GND pins, and wired to pins 0 and 1 for data.
A very handy feature of the motor driver is that you can feed it with 9V and it will output 5V (from the 5V pin). So the battery goes into the VCC and GND, and then the Netduino is powered from the GND and 5V on the motor driver board. The LED board can also be driven directly from that 5V.
Each motor is connected to the outputs on each side.
Lastly, all the actual driver pins need to be wired to the Netduino. The purple and blue wires are connected to PWM pins on the Netduino and these are used to tell the motor driver how fast to make the left/right motor. The four remaining white wires are connected to digital Netduino pins and are used to tell the motor driver which direction to move the motor in.

board

From left to right the direction pins on the motor driver are labeled INA, INB, INC, IND – meaning that each is an input. The left motor is controlled by INA and INB, while the right motor is INC and IND.

To make the left motor (same for the right except with INC and IND) go forwards you set INA HIGH, and INB LOW. To make it go backwards you set INA LOW, and INB HIGH. To stop the motor set both LOW.
Obviously the speed that it spins forward or backwards is defined by the purple/blue wires mentioned earlier.

Directly to the right of the lower power points there is a white button – this is the on/off button for the board, so doubles up as a way to turn the entire car on and off without having to unplug the battery.

 

Netduino code

If you haven’t followed my Netduino+WP8 Bluetooth tutorial then go here now, because the Bluetooth part won’t be explained fully here.

Before adding any Bluetooth code, add a new class to the project called MotorController.cs.
This class will basically control one motor per instance, and is just a helper to set the speed and direction.

I found that these motors didn’t move at all (or VERY slowly) until I fed the PWM signal pin with at least 70%. To counter that I scale the incoming speed percent to between 70 and 100%. So if you tell the motor to go 1% then it will actually go just above 70%.

The Direction enum is just to have a nice way to define direction.

If you are on the latest version of .NETMF then you will need to add the following line to the namespaces at the top of the file:

Plus you will need to add the SecretLabs.NETMF.Hardware.PWM assembly to your project.

Now navigate back to Program.cs where we need to add the Bluetooth stuff, as well as creating an instance of MotorController for each motor.

So change the current code there to look like this:

The pins used to control the motor driver are based on the wiring diagram higher up – you can change them and it will still work as long as you ensure the Purple/Blue wires go to PWM pins, and the white wires go to digital pins.

Below that method we add the event handler for when the serial Bluetooth module receives data:

And lastly, when that method detects that an end-line character has been reached (“|” (a pipe) in this case), it will send the complete string to this next method:

As you can see above, the Windows Phone will send through commands in this format: direction,left speed, right speed. Along with forwards and backwards, you might notice “spin”. This spins the wheels in opposite directions to make the car turn on the spot, but doesn’t work particularly well due to the friction mentioned at the end of this post.

Build that code and deploy to the Netduino, then we can move on to the Windows Phone part!

Windows Phone code

Create a standard Windows Phone 8 application. Before you forget, enable the ID_CAP_PROXIMITY capability within WMAppManifest.xml.

Next we need to create a control stick that will be used to move around and drive the car. Create a new UserControl called ThumbStick.xaml within a new folder called Controls. The XAML below just defines a square with an X in the middle. There are events to detect when the user puts their finger down, moves it, and then lifts their finger. You might notice that instead of referring to a finger, it mentions a mouse – this is just because it is based on desktop Silverlight.

The RenderTransform is used to make the X move around with the users finger.

Now go into ThumbStick.xaml.cs to add the code.
Firstly, add the event handlers for the touch events:

Also notice that the first line defines an event that this control will fire each time a finger is moved.

Now we just need the method that will work out where the users finger is, move the X in the UI, and then work out how fast each motor should be.

The most complicated part of that code is working out the speeds of each motor based on how much it is turning. The way I have done it is that when turning left, the left motor will always be 100%, but the more left they turn, the slower the right motor will turn. So moving the thumbstick 20% left will set the left motor at 100% and the right motor at 80%. Moving the thumbstick 70% left will set the left motor at 100% and the right motor at 30%.

Note: All the % speeds above are then scaled based on the speed value.

Now go to MainPage.xaml and add the following line into the XML namespaces at the top:

Change the names accordingly if you project or controls folder is different.

Now lets define the UI:

Screen

All that is left to add is the code. Open up MainPage.xaml.cs.
Add the following boiler-plate Bluetooth code to handle the receiving and sending of data:

The last thing to do is to add the event handler for when the ThumbStick moves, and write the value to the Bluetooth module. The code above automatically adds the pipe (“|”) character to define the end of the message.

And that is everything!

Make sure the Netduino code is deployed to the Netduino, then wire everything up and turn it on. Once it is on, pair your Windows Phone with the BT module and then open up the app. Give it a few seconds to connect and then start driving around :).

Considerations

I wanted to keep the mechanics very simple so decided to not have a turning mechanism, but instead slow down a specific motor to make it turn in that direction. Although this works, there is a lot of friction because the back wheels are forced to slide out when turning. In future I would probably add a way for the wheels themselves to turn, or go with a tank-tread style.

I designed the platform around using a 9V battery – which turns out is a pretty bad idea. Compared to most other types of batteries they have a very low mAh and die really fast when driving two motors and an array of LED’s.

 

You can grab the source code and .STL 3D printing files HERE!

car2

If you enjoy Bluetooth and Windows Phones, checkout my other posts on the topic.

Got questions? Ask me in the comments below or on the Twitter machine: @roguecode

Tweet about this on TwitterShare on FacebookShare on RedditShare on Google+Email this to someonePin on Pinterest
  • Pingback: 3D Printed Netduino Remote Controlled Car()

  • Pingback: 3D Printed Netduino Remote Controlled Car | Hack The Planet()

  • Patrick

    I love your Windows Phone controlled Car! That’s my new Weekend Project. Thanks for you great Blogpost =D

  • Erick Calderin

    Could I do it, using an arduino?

    • Patrick Herting

      Yes! You can wired self like Netduino, but the code for netduino are different like Arduino. But you can easy rewrite the code for Arduino Boards. The Windows App can be the same, because the app doesn’t interesset which platform he send the data.

    • RogueCode

      Sorry, I somehow missed this comment!

      As Patrick mentions below, yes, it is totally possible to use with Arduino. There is some Arduino Bluetooth code in this post (http://blog.roguecode.co.za/Creating-Bluetooth-game-controller-Windows-Phone-ATTiny-Arduino) that may help you out.

  • Rillist

    Ordered my stuff today, gonna tackle this myself. Sounds fun!

    I’m not sure if it’s possible with these components, but I am planning to implement turning a bit different. When turning left I want to roll the left wheel backwards, and the right wheel forwards. This should produce almost 0 radius turns. If the back wheels had motors too, this would definitely produce 0 radius turns.

    • RogueCode

      That is sort of how a tank moves. It will sort of work here, but only because the car is so light. The ideal solution would be to do what you described, but also have an axel on both the front and back wheels to turn them.

  • Prabhu

    I am using Netduino 1 to drive the motors and have used 12v to power up the motor driver and have wired up as shown above. But still the motors are not moving when I send the commands through Nokia 520 mobile using Bluetooth. Can anyone please help me here?

  • Prabhu

    When I debugged the data received from Windows mobile seems to be a very long string and it fails to execute DoSomething() method

    • RogueCode

      Can you show me what the string is?

      • Prabhu

        sure give me 2 mins

      • Prabhu

        The string looks like this “b$À &&xDb”

      • Prabhu

        Also “b b x@b ( x@f0( x@b f xBf0( x@b b x@b b xBf0( xBf b x@b bþ@b b”x@b”b x@b” x@b”b x@b”b$xBf2($x@f”b$x@b0($xBf0b$x@b b&xBf0b&x@b b&x@b b&x@b (&xB”

      • Prabhu

        And in DoSomething() method, the below condition fails
        string[] split = buffer.Split(new char[] { ‘,’ });
        if (split.Length == 3)

        • RogueCode

          OK the data is definitely coming through incorrectly, causing it to fail. There are a few possible solutions to this, sorted in the order you should try them:

          1) Switch around the TX and RX wires from the Bluetooth module. They may have been marked with the wrong labels. Make sure not to switch power.

          2) On this line: _bt = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One);
          try change the 9600 to 38400. If that doesn’t work, try 115200.

          3) If those don’t work it is likely that the module either works differently, or is damaged. You might want to try a different module. Someone recently emailed me with a similar problem to yours, and it seems like trying an older revision of the Bluetooth module worked.

  • Prabhu

    Q: I am using 6F22 9V battery to power up my Netduino. But I am seeing that PWR LED on board is not turning on? Am i doing the right to power my Netduino?

    • RogueCode

      Yes, that should work fine. Are you powering it via the barrel connector? Try with USB and make sure it turns on. If that doesn’t work either, your board may be dead. But from your below questions, it seems that your board is working.

  • Prabhu

    I am getting the proper string now. I had connected bluetooth module power to 3V3 of Netduino. But instead now I connected it to 5V of Netduino and its giving me correct string and DoSomething() method works. But still can see motors are not moving. I have used 150rpm geared motors

    • RogueCode

      I’d recommend getting just the motors working by themselves with the Netduino and motor board (i.e. leave out all the bluetooth stuff).
      I’m not sure why it isn’t working. You’ll need to re-check all your connections and find instructions for the specific motor driver you have.

      • Prabhu

        I am using the same motor driver as suggested here. May be i will check the connections once. Thank you

  • Prabhu

    Are pins 9 and 10 on Netduino are used for PWM?

  • Prabhu

    Baud Rate i have maintained to 9600 only, all other values are not working

  • Prabhu

    Hello Team,

    In L298N I am able to see A, B, C and D LEDs glowing, but when I send directions from Bluetooth enabled Nokia mobile, I can see some of the LEDs glow on and off in L298N but the below code fails to move the motors

    _signalPort.SetDutyCycle(finalVal);

    Please suggest some ideas to make it work

  • Prabhu

    Here the code is written for Netduino 2. But I am using Netduino. Do i need to change any code for Netduino board