Sunday, March 25, 2012

Making Your DROID Into Your Microprocessor: Part 1


Whether you’re an android developer and want to get into microcontrollers, or you know your microprocessors but want to dive into android apps; the IOIO has something for you. SparkFun’s IOIO (pronounced yo-yo) brings I/O ports to your android smartphone and is programmed via java, in a similar layout to Arduino. The processing is done on the phone, giving your processor incredible specs, and access to all of the your phone’s sensors (accelerometer, gyro, compass, GPS, camera, etc). The tutorial and app below takes you one step after Sparkfun’s walkthrough and shows you how to successfully use digital input, digital output, pwm output, and UART communications regardless of your programming experience. Download the source and the app to start learning!

The Set-Up

To start out, boot up the app to test out your IOIO. You can connect the two via USB or a bluetooth dongle (a list of working dongles is located HERE - the initial pairing password is ‘4545’). The IOIO does NOT receive power through your phone, so an external power source is needed, anywhere from 5V -15V, if necessary it can be easily powered through the 5v pin of your arduino. Click the top toggle button and you should see your on-board LED flash on. If not then your IOIO and phone aren’t connected or are incompatible.   

Next you want to connect a wire from GND to digital pin 14. You will see the Digital Input on your phone go from High to Low. This is because the pins on your digital pin are normally high - meaning if you were to connect a button to it, you would need to connect it in-between GND and the input pin, rather than 5v. This holds true for all of the pins on the board as well, so it is important to remember that when designing your circuits (luckily the IOIO offers 9 GND pins).

The next thing to test out is the IOIO’s PWM capabilities. The program default outputs to pin 13, but any of the ‘P’ pins (located on the underside of the board) can be used with a little change to the code. Hook up an LED and a resistor onto a breadboard and connect your circuit to GND and pin 13. When you move the slider in the app the  resistor’s brightness should fade accordingly. This is done through an output type called Pulse-Width Modulation. By outputting Highs and Lows at a rapid speed you can change the simulated voltage according to the percent of logical highs there are.

The final capability of the app is sending UART (serial) commands. If you hook up pin 10 and 11, you can make your IOIO send itself commands over UART and then show you the values on your phone. UART is useful when using sensors that require a Serial Input (LCD Screen), or  output a Serial String (RFID Reader). UART is more complicated than standard I/O but is common when attaching higher-end sensors, or communicating between two microcontrollers (IOIO and Arduino maybe?).

The Code

When you are ready to start programming you can unzip the code located HERE and follow Sparkfun’s tutorial up until the Importing the HelloIOIOExample. When you get to that point you want to import the code the same way as their example but use the code that you just downloaded. The code was also written over the HelloIOIO example, so if you choose, you can copy and paste it over the current HelloIOIO example.

Digital Output -

So let’s dive into the code and discuss how you can use these features on the IOIO. Digital Output is probably one of the easiest commands to do. In the code we first declare the button as  ToggleButton = DigOutToggle; under the MainActivity function. We then assign the DigOutToggle to reference the button in the XML file (the part of code that dictates your app’s layout) -  DigOutToggle = (ToggleButton) findViewById(R.id.onboardbutton); which makes the state of togglebutton be stored into DigOutToggle. Next, we need to ‘open’ the pin to make it into an output, then make the togglebutton control the pin’s state. To do so we declare digout as a digital output DigitalOutput digout;, and assign it to be an output using the ioio_.openDigitalOutput(digoutpin); command. Now, whenever we set digout variable to be ‘True’ the pin will be set High, and whenever we set the pin ‘False’ it will be pulled to ground. Under the loop function we set digout to write the state of the toggle button using this line: digout.write(!DigOutToggle.isChecked());. By using these commands and changing a few variables, you can set any of the IOIO’s pins to be set as digital outputs. For more information check out the wiki HERE.

Digital Input -

Digital Output follows almost the exact format as above except for a few obvious changes (output to input). You just have to remember that the microcontroller will normally see pins as HIGH! So you need to connect buttons etc to GND instead of 5v to read the state!

-Another post with UART / PWM tutorials will be uploaded soon-




18 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. This comment has been removed by a blog administrator.

    ReplyDelete
  9. This comment has been removed by a blog administrator.

    ReplyDelete
  10. This comment has been removed by a blog administrator.

    ReplyDelete
  11. This comment has been removed by a blog administrator.

    ReplyDelete
  12. This comment has been removed by a blog administrator.

    ReplyDelete
  13. This comment has been removed by a blog administrator.

    ReplyDelete
  14. This comment has been removed by a blog administrator.

    ReplyDelete
  15. This comment has been removed by a blog administrator.

    ReplyDelete
  16. This comment has been removed by a blog administrator.

    ReplyDelete
  17. This comment has been removed by a blog administrator.

    ReplyDelete
  18. This comment has been removed by a blog administrator.

    ReplyDelete