Android + IOIO + Accelerometer Servos

  

A robotic gripper with wrist rotate controlled by Android on-board accelerometer.

For additional background information on interfacing Android with IOIO, check out my other introductory tutorials:

Android + IOIO + Simple Digital Output
Android + IOIO + Simple Digital Input
Android + IOIO + Simple Analog Output
Android + IOIO + Simple Analog Input

Background on Android development, IOIO, and electronics:

Meet IOIO
IOIO for Android Beginners Guide
IOIO Wiki
Android Developer’s Guide

 

Hardware

Parts needed:

  • Android Device (1.6+, 2.1 for Bluetooth)
  • IOIO (available at Sparkfun)
  • 2x Hobby servo
  • 2x 10k ohm resistors
  • Lynxmotion gripper (available at Robotshop)
  • Breadboard
  • Power supply
  • Hook-up wire

 

Assembly

Connect the red, power lines of the servos to +5v, the black ground lines to GND, and the yellow signal lines to the desired output pins, number 3 and 6 in the example below.  Other pins can be used as long as they support peripheral output (for PWM, marked with the letter ‘p’ on the back of the IOIO) AND are 5V tolerant (marked with a black circle around the pin). This leaves pins 3-7, and 10-14 as the only potentials.  Also, connect the same signal lines to +5V, with a 10k ohm resistor in series.  This allows use of the pins in 5V open drain mode, required since the IOIO operates with 3.3V. Here is a diagram of the completed circuit (created with Fritzing):

 

Software

Get the source

With the circuit assembled, the next step is to get the demo application on the Android device. You can either download the pre-built .apk or checkout the source from Github:

git clone git://github.com/mitchtech/android_ioio_accelerometer_servos.git

If you are building from source, you will also need to import the IOIO Library project, and optionally the IOIO Bluetooth library projects, both available here:

git clone git://github.com/ytai/ioio.git

 

Install, connect, profit!

Finally, upload the app to the Android device (or browse to this page on the device and download the apk above). Connect the device to the IOIO, and start up the app.

 

6 Comments

    • The link should be working now, sorry about that… Thanks for pointing it out sailoog!

      • package com.example.kyoshoclient;
        import android.os.Bundle;
        import android.app.Activity;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.view.View.OnTouchListener;
        import android.widget.Button;
        import android.widget.EditText;
        import android.util.Log;
        import android.widget.SeekBar;
        import android.widget.SeekBar.OnSeekBarChangeListener;
        import android.widget.TextView;
        import java.io.*;
        import java.net.*;

        public class ClientActivity extends Activity implements OnSeekBarChangeListener {
        private SeekBar bar, barD;
        private TextView textProgress, textDirection, textWord;
        private EditText serverIp;
        private String serverIpAddress = “”;
        private Button connectPhones;
        private InetAddress serverAddr;
        private boolean connected = false;
        private DatagramPacket direct;
        private DatagramPacket move;
        private DatagramSocket sender_socket = null;
        private DatagramPacket sender_packet = null;
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.client);
        textProgress=(TextView)findViewById(R.id.textVieww);
        textDirection=(TextView)findViewById(R.id.textViewww);
        textWord= (TextView) findViewById(R.id.textViewwww);
        serverIp = (EditText) findViewById(R.id.editText1);
        connectPhones = (Button) findViewById(R.id.button3);
        connectPhones.setOnClickListener(connectListener);
        DatagramSocket socket = null;
        try{
        socket = new DatagramSocket();
        }catch (Exception ex){
        Log.e(“error”,ex.getMessage());
        ex.printStackTrace();
        }
        String movement = (“textProgress”);
        String direction = (“textDirection”);
        try{
        bar= (SeekBar)findViewById(R.id.seekBarr);
        DatagramPacket move = new DatagramPacket(movement.getBytes(), movement.length(), serverAddr, 2390);
        sender_socket.send(sender_packet);
        bar.setOnSeekBarChangeListener(this);
        bar.setMax(255);
        bar.setProgress(127);
        barD= (SeekBar) findViewById(R.id.seekBarrr);
        DatagramPacket direct = new DatagramPacket(direction.getBytes(), movement.length(), serverAddr, 2390);
        barD.setOnSeekBarChangeListener(this);
        barD.setMax(255);
        barD.setProgress(127);
        }
        catch (Exception ex) {
        Log.e(“error”, ex.getMessage());
        ex.printStackTrace();
        }

        Thread mThread = new Thread(mRunnable);
        mThread.start();
        }

        private Runnable mRunnable = new Runnable() {
        public void run() {
        byte[] buffer = new byte[256];
        while(true){
        try {
        DatagramSocket receiverSocket = new DatagramSocket(2390);
        DatagramPacket receiverDatagramPacket = new DatagramPacket(buffer, buffer.length);
        receiverSocket.receive(receiverDatagramPacket);
        Log.v(“receive”, new String(buffer,0,receiverDatagramPacket.getLength()));
        buffer = new byte[256];
        } catch (Exception ex) {
        Log.e(“error”, ex.getMessage());
        ex.printStackTrace();
        }
        }
        }
        };
        public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
        { switch (arg0.getId())
        {
        case R.id.seekBarr:
        if (arg2)
        {
        if (arg1 == 127)
        textProgress.setText(“speed: “+ arg1);
        if (arg1 127)
        textProgress.setText(“speed: “+arg1);
        }
        break;
        case R.id.seekBarrr:
        if (arg2)
        {
        if (arg1 == 127)
        textDirection.setText(“speed: “+ arg1);
        if (arg1 127) {
        textDirection.setText(“speed: “+ arg1);
        }
        break;
        }
        }
        }
        }
        public void onStartTrackingTouch(SeekBar seekBarr) {
        textWord.setText(“ONLINE”);
        }

        public void onStopTrackingTouch(SeekBar arg3) {
        { switch (arg3.getId())
        {
        case R.id.seekBarr:
        {
        bar.setProgress(127);
        textProgress.setText(“127″);
        }
        break;
        case R.id.seekBarrr:
        {
        barD.setProgress(127);
        textDirection.setText(“127″);
        }
        break;
        }
        }
        }
        private OnClickListener connectListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
        if (!connected) {
        serverIpAddress = serverIp.getText().toString();
        if (!serverIpAddress.equals(“”)) {
        Thread cThread = new Thread(new ClientThread());
        cThread.start();
        }
        }
        }
        };

        public class ClientThread implements Runnable {
        public void run() {
        try {
        serverAddr = InetAddress.getByName(serverIpAddress);
        Log.d(“ClientActivity”, “C: Connecting…”);
        } catch (Exception ex) {
        Log.e(“error”, ex.getMessage());
        ex.printStackTrace();
        }
        }
        }
        }
        how come unfortuanlty it has stop working?

  1. Very nice dude…

    I have also found one good link here….

    Accelerometer Basic Example – Detect Phone Shake Motion

  2. I’m typing this at 01/06/2014. This means that more than 2 years have passed since the upload of this video.
    There’s no doubt that this kind of tech has evolved significantly during those 2 years. All input is welcome, please share ANY information/ evolving/ reformation/ invention/ etc with me: mr.coldplace@gmail.com – I will translate all into tutorials so that people who are really interested about this stuff, can be educated about these thing in a very efficient way.
    Linking hardware with software is the future.

  3. gary cahill

    Hi
    Im trying to build a pan tilt camera control that works of an accelerometer over wifi. Distance up tomorrow (3.5miles approx). If anyone has any comments or help please post here or email me at gary_cahill@hotmail.com
    Thanks

Leave a Reply