Category Archives: Internet of Things

Moving a Car Forward/Backward

Well, because I got bored using Processing based default Arduino IDE, I have installed Visual Micro a very rich Visual Studio extension that offers you a fantastic integrated environment for Arduino based development that boasts powerful debugging capability, that too is free.

Untitled

Not to mention state of the art code completion and intellisense:

Untitled1

In today’s post, I would like to keep a record of how I have assembled a car chassis and made two DC motors move forward and backward controlled by an Arduino Uno and driven by a L293D IC. I have bought Magician Chassis that comes with two DC motors. A DC motor is simple and its capability is also limited, such as moving forward and backward at a specific speed. The L293D IC can set a speed to two motors at a time and change their directions when needed. I have passed unregulated voltage to the motors and I didn’t care; I just wanted to get up and running with the motors, hence you may expect this setup may damage your motors if you run for a long/little while. I’m also powering the motors as well as the L293D IC with an external 9V battery in order to draw less from the Arduino while I am giving the Arduino only 6V via 4 x 1.5V AA batteries. The IC will specifically run the motors with the power applied at its VSS (Pin 16).

Configuring the L293D

I have put Pin 8 and 16 safely into +9V since 8 and 16 are VCC and Pin 1 is Enable 1 and Pin 9 is Enable 2. Enable pins actually enable the motors. For example, if Pin 1 is put on the ground the motor connected to the left side of the IC will cease to work. If Pin 9 is put into ground, the motor connected to the right side of the pins will cease to work. Therefore, what we are going to do is that we are going to use these pins as speed controller. More about that later. I have also safely put Pin 4, 5, 12 and 13 into the ground. Other pins are setup as below:

Motor connected to Pin Motor connected to Pin
Left 2 (Input 1) Right 10 (Input 3)
  3 (Output 1)   11 (Output 3)
  6 (Output 2)   14 (Output 4)
  7 (Input 2)   15 (Input 4)

Here’s the truth table based on which the motor will change direction:

Left motor Pin 2 Pin 7 Right motor Pin 10 Pin 15
Clockwise Low High Clockwise Low High
Anti-clockwise High Low Anti-clockwise High Low

Connecting the DC Motors to L293D

Left motor negative Pin 3 Right motor negative Pin 11
Left motor positive Pin 6 Right motor positive Pin 14

Setting up the Arduino

I have previously mentioned that Pin 1 and 9 are Enable pins and they will allow us to control the speed of the motors as well. Therefore, these need to be connected to Pulse Width Modulation (PWD) ports of an Arduino because we need to be able to pass analog values between 0-255. Notice the following Arduino setup of all the components along with Pin 1 and 9 of L293D.

Arduino Pin L293D Pin
3 1
5 15
6 10
9 9
10 2
11 7

This is how it looks like:

DcMotorAheadReverse_bb

The code

Here’s the code that runs the motors forward. If you would like to run it backward, just pass false instead of true into the move function that was written below. You will also notice that I have specified speed = 255 which is the max, and min = 0.

int speedPin1 = 9;
int speedPin2 = 3;

int in1 = 10;
int in2 = 11;
int in3 = 6;
int in4 = 5;

int speed = 255;

void setup()
{
	pinMode(speedPin1, OUTPUT);
	pinMode(speedPin2, OUTPUT);

	pinMode(in1, OUTPUT);
	pinMode(in2, OUTPUT);
	pinMode(in3, OUTPUT);
	pinMode(in4, OUTPUT);

	analogWrite(speedPin1, speed);
	analogWrite(speedPin2, speed);
}

void loop()
{
	move(true);
}

void move(boolean forward)
{
	digitalWrite(in1, !forward);
	digitalWrite(in2, forward);

	digitalWrite(in3, !forward);
	digitalWrite(in4, forward);
}

Final Outcome

WP_20141108_23_23_08_Pro

Buzz like a UPS

This is yet another simple test of a new hardware component that I haven’t played with before. Buzzer is an audio device which may be mechanical, electromechanical, or piezoelectric. It can generate from quite awful UPS-like siren to melodious tone; it really depends on how you want to use it. I just wanted to make noise like that of a UPS does when electricity goes off.

The rest of the post can be found here.

Blinking BiPolar LED

BiPolar LEDs have 3 three legs. One goes to ground, and the other 2 represents two different colors in the same LED. In my case, they are green and red. I have also used a variant of Arduino Nano in order to reduce the size of the experiment. Here’s the list of connections that I have made:

LED’s ground leg 220 ohm resistor
220 ohm resistor Arduino’s ground
LED’s one leg Arduino’s Digital Pin 12
LED’s other leg Arduino’s Digital Pin 13

My intention was to blink the colors alternatively. Green, red, green, red, green… with a 1-second delay in between. The code is as simple as one can imagine. There’s really nothing novel going on here:

int green = 12;
int red = 13;

void setup() {                
  pinMode(green, OUTPUT);     
  pinMode(red, OUTPUT);
}

void loop() {
  digitalWrite(green, HIGH);
  digitalWrite(red, LOW);
  delay(1000);           

  digitalWrite(green, LOW);
  digitalWrite(red, HIGH);
  delay(1000);   
}

Here’s the final outcome:

WP_20141105_21_04_47_Pro

Hello Blinking LED

Apparently the post title here is the universal Hello World for physical computing. I am a newbie and had no prior knowledge in electronics. I will try to write a few posts which will serve as a work log for me so that I can revert to these when needed/forgotten due to focusing on other priorities of life over time.

Wiring up an Arduino

I’m using a Seeeduino ADK (a Arduino Mega 2560 variant) for this post. I have simply plugged it to the computer via Micro-USB cable. I have also used a breadboard and connected a LED and a 220 ohm resistor between ground and pin 15. The register here prevents the LED from taking a damage due to 5V on which Arduino runs on.

LEDblink_bb-cropped   

WP_20141104_18_46_01_Pro

You will notice that without doing anything the LED blinks randomly along with another LED which is in-built.

Blinking from a Program

How about we write a program that can systematically after each second blinks the LED? Download and install the Arduino software from here which comes with a minimal yet effective IDE. I’ve written the following code which is quite self-explanatory:

 Untitled

I’ve pressed Control + U, then it took a few seconds to upload to the processor, and voila! The program that I have written is now successfully making the LED blink every accurate second.

Blinking Two LEDs in a Particular Order

Now I would like to exercise more power of code and blink 2 LEDs, red twice each 2 seconds and green just once when red is done. I have organized the circuit in a slightly different way now:

DoubleLEDblink_bb-cropped I have used pin 13 and pin 12 now in order to blink green and red in turn, and written the following code:

int green = 13;
int red = 12;

void setup() {                
  pinMode(green, OUTPUT);     
  pinMode(red, OUTPUT);     
}

void loop() {
  digitalWrite(red, HIGH);
  delay(1000);           
  
  digitalWrite(red, LOW);
  delay(1000);   

  digitalWrite(red, HIGH);
  delay(1000);           
  
  digitalWrite(red, LOW);
  delay(1000);           

  digitalWrite(green, HIGH);
  delay(1000);               
  
  digitalWrite(green, LOW);
  delay(1000);               
}

Here’s the final outcome:

WP_20141104_19_46_02_Pro