Transistors for Robotics: Arduino Basics

Transistors for Robotics: Arduino Basics

Your powerful friend, the transistor can control high-voltage motors and lights from a microcontroller pin.

  • 8,240 views
  • 5 comments
  • 42 respects

Components and supplies

Apps and online services

About this project

Do you need to control something high-powered from an Arduino pin? Do you need to control something from a sensor that just doesn't give much output? Want to toggle power to a hacked device, replacing or augmenting a physical button? The transistor has got your back. But before we get to hacking, let's clear up a few things:

Good Stuff to Know

  • BJT vs. (MOS)FET: These are different types of transistors. I'm working with bipolar junction transistors (BJT). FETs (field-effect transistors) usually look a bit beefier, with a built-in heatsink. Each has three pins, but BJTs use the terms "collector, base, and emitter" for them, while FETs call them "source, gate, and drain".
  • Collector: Similar to the positive leg on an LED, this is where power flows in.
  • Base: This is the "trigger" pin coming from your controller, sensor, or whatever.
  • Emitter: Like the negative leg on your LED, this is the ground side of your transistor.

Even transistors of similar types may put these legs in a different order, so double-check your part! An easy way is to Google image-search "[part number] pinout".

  • Part numbers: Make sure you grab the right transistors! You can Google the datasheet to be safe. Things to watch out for: it must be able to handle the power you're pushing through; double-check which way to hook it up; and it should be the right type (NPN or PNP). Use the first two rows engraved into the transistor. They can be hard to read; sorry.
  • NPN vs. PNP: NPN transistors are normally "off" (disconnected), unless you're applying power to the base pin. PNP transistors are normally "on" (allowing current to flow), unless your signal is high.
  • Transistor vs. Relay: If you're toggling something that's really power-hungry, consider using a relay instead. They're a bit slower, because they use a magnet to toggle a physical metal switch inside a little box. But that also makes them a bit safer, because of the physical isolation between the leads. And they make a cool little "click" noise. Rad!

The Code

We'll just run the Arduino Blink sketch, since that gives a great visualization of what's going on.

The Setup

I'm using a DC motor for this example, since they tend to ride the edge between what an Arduino pin can toggle on its own, and needing a little help. As you can see in the video above, it just barely works. DC motors are common in wheeled robots and R/C vehicles. They have two terminals, and one gets connected to power, the other to ground. Based on which way 'round you put the wires, the motor will spin clockwise or counter-clockwise.

Try plugging one wire directly into pin 13, and one directly into ground, to start with. Not much going on there, eh? Little haptic motors (and their twins, cell-phone vibe motors) will run perfectly happily on the small power supply. But kick it up to small-robot standards, and you'll have to prod the motor to get it spinning even when the power's on.

Now, unplug your Arduino and reconfigure the circuit on a breadboard: plug one of the motor's wires into the 5V pin on your Arduino, if there is one; otherwise, skip ahead to the next bit with the external battery. Now, connect the transistor:

  • BASE (middle) leg to digital pin 13
  • COLLECTOR to the motor's other wire
  • EMITTER to a ground pin

Plug in the 'Duino again, and voilà! Your little motor should be going like crazy, then off and on again, just like you'd expect. Make sure your motor is ready to handle that much voltage, and unplug it quickly if you smell anything unusual.

External Power

OK, cool! We can power stuff directly from the digital pin, or use that to control 5V from the Arduino. The next step is to add an external source – for example, in case you want to control a light that takes 9V or 12V.

The main thing to note is that you're tying both grounds together: the one from the power source, and the one for the controlling signal, meet to form a "common ground". This allows both power and data signals to flow through the same circuit.

Your motor's chosen "ground" wire is still tied to the collector; your digital control pin is still tied to the base; and GND is still tied to the emitter. But this time, the motor's designated "power" wire is sourced from the positive terminal of your 9V source, and the battery's negative terminal is tied to GND as well.

This circuit can also be used to amplify the amount of power to control a heavy-duty servo, or with NeoPixel strips that want to run on their own power source, or anything similar!

Next week, we'll cover how to drive your motor forwards and backwards – it takes a bit of finagling with an H-bridge, but it's not bad!

And in our next transistor tutorial, we'll get to toggle buttons and switches (to automate devices for home automation). We'll also get around to amplifying the signal from a piezo disc – which is useful for making impact-reactive systems like light-up wearables!

See the whole series of Hackster 101 tutorials on Hackster and YouTube!

Code

Arduino example sketch: "Blink"C/C++
The basic on/off "Blink" sketch from the Arduino IDE.
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the Uno and
  Leonardo, it is attached to digital pin 13. If you're unsure what
  pin the on-board LED is connected to on your Arduino model, check
  the documentation at http://www.arduino.cc

  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
 */


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

Schematics

1. 5V motor control with Arduino
Qggdkifzmklwvnh2aji1
2. 9V motor control with Arduino
Make sure your transistor is designed to handle this much power! If not, you might end up with an always-on device.
Wk5hhyg8m9eyzbdujp1c

Comments

Similar projects you might like

NeoPixel LEDs: Arduino Basics

by Alex Glow

  • 43,196 views
  • 1 comment
  • 46 respects

Button: Arduino Basics

by Alex Glow

  • 10,991 views
  • 0 comments
  • 11 respects

Robotic Arm from Recycled Materials

Project tutorial by circuito.io team

  • 24,661 views
  • 22 comments
  • 97 respects

Servo: Arduino Basics

by Alex Glow

  • 16,809 views
  • 2 comments
  • 59 respects

LED Blinkies: Arduino Basics

by Alex Glow

  • 5,612 views
  • 1 comment
  • 11 respects

Robotics LED Strip

Project tutorial by Arduino “having11” Guy

  • 2,815 views
  • 3 comments
  • 13 respects
Add projectSign up / Login