Turn a DC Motor From an Android Phone Using App Inventor #tt25

Tinkerer: Lory Livezey

In this tutorial, you'll learn how to hook up a DC motor and turn it in either direction by pressing a button on your android phone, tablet or Kindle.


Objectives

You'll learn and practice how to: *


Previous Step

  Connect Your Android Phone to Arduino Using App Inventor #tt23


What you will need

Android Device or Kindle PC, Mac or Raspberry Pi


Starter Breakpoint

The Android Project (continuing from #tt23):

The App Inventor Project:


Where We Left Off

Let's recap the hookup we have so far. This is how we connected the HC-06 bluetooth module to the Arduino in #20. You can either connect up using this diagram, or click here to go back:

Previous Project Diagram

In App Inventor, we created buttons to connect and disconnect from the bluetooth module. We then created buttons to simulate our X and Y axes of the gyroscope that we're going to add on next.

Previous Project Starting

In blocks view, we're sending 111 to the Arduino if you press X and 222 if you press Y. We're not going to make any changes here.

Blocks View


Connecting the L298N Motor Driver

Here's our hookup:

Hookup Diagram

You'll plug the DC motor into the Motor A pins. Don't worry about which is which just yet. It only determines the direction of your motor, and you can switch them later.

Next, plug the battery into the 12V pin, and 12V really means a max of 12 volts. You'll do just fine with 9. I'm using 3 3.2v LifePo4 batteries, which have 9.6 v. I prefer these over a 9v battery because they last longer, and these are rechargeable. You can use a 9V battery, or a battery pack with at least 4AA batteries. The motor will run with less voltage, but it won't turn as fast. Plug the ground of the battery into the Ground Pin, and also run another ground wire to the Arduino. I find it's best to twist the 2 ground wires together.

The Enable pins allow us to set the speed of the motor. Connect EN1 to pin 10.

The Next 2 are Logic pins, and they are for direction. Plug the first one into pin 9 and the second into 8. Project Hookup


The Arduino Sketch

Open the Arduino sketch from the last tutorial.

First we'll set up the pins as Integers and we used 10,9, and 8. If you remember, we're getting a value like "x=111" from the app, and we're parsing that out. We'll turn the wheel in one direction when we press the X button on the app, and the other direction when we press Y.

Greater Than Tag Missing and End Tag You can scroll down to find the entire sketch that you can copy/paste


// set up the motor pins 
int enA = 10;
int in1 = 9;
int in2 = 8;

Here are the specs for the L298n motor driver, pretty heavy reading! But we don't need to know how a TV works inside to watch it, we only need a remote, right? I'm not saying you won't want to learn that stuff eventually, i'm just saying let's have some fun first! That's the great thing about IoT libraries. Others have done your heavy lifting for you (thanks, Adafruit!) Basically, setting one of the pins to high and the other to low will turn the motor in one direction, then switch them around and it will turn in the other direction.

We'll set the ENA pin to the speed sent by the app, which will be 111 or 222. 255 is the max speed of this motor. These lines will spin the motor in one direction, at the speed of x.

// turn the wheel one direction to the speed of x
analogWrite(enA,x);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);

By switching the HIGH and LOW we'll turn the direction. These lines will spin the motor in the other direction, at the speed of y.

// turn the wheel in the other direction to the speed of y
analogWrite(enA,y);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

Here is the entire sketch:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(3,2);  // Rx, Tx

// set up the motor pins
int enA = 10;
int in1 = 9;
int in2 = 8;


String value = "";
int ledpin=13;
float x = 0;
float y = 0;


void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  pinMode(ledpin,OUTPUT);
}

void loop() {

  while(mySerial.available()) {

    char character = mySerial.read();
    Serial.println(character);

    if (character=='x' or character=='y')
    {

      Serial.print(character);
      Serial.print("=");
      Serial.print(value);

      if (character=='x') {
        x=value.toFloat();
        // turn the wheel one direction to the speed of x
        analogWrite(enA,x);
        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
      }
        
      else {
        y=value.toFloat();
        // turn the wheel in the other direction to the speed of y
        analogWrite(enA,y);
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
      }
        
      x=0;
      y=0;
      value="";
    }
    else
    {
      value.concat(character);
    }
  }
  

}


Test in AI Companion

Switch back to App Inventor, and connect to the AI2 Companion on the Kindle.

Connect App Inventor

Type or scan the QR Code:

Scan QR Code

Connect to the HC-06 Bluetooth Module.

Connect HC-06 Bluetooth

If it doesn't connect, go into your settings and make sure it's connected in there.

Press the X button and the motor will turn one way, and Y will turn it the other way.

Final Project


Finish Breakpoint

The Android Project:

The App Inventor Project (no changes were made):


Conclusion

In this tutorial, we built on the last by adding the L298 Motor driver to our project, and spinning our motor using the buttons on the Android. Next, we'll start using these skills in a real project!


Next Up

Assembling the Tinker Pi Robot for Arduino #tt26

Be sure to subscribe to our channel!