Archive for April, 2008

Controlling Servos with Arduino

Here’s the one minute overview of how you control servos. Most servos rotate 180 degrees. Servos are controlled by a single digital pin. The way to tell a servo what to do is by sending them a high pulse for a certain duration. Most standard RC servos use a pulse of 1 millisecond to indicate 0 degrees of rotation and 2 milliseconds for 180 degrees. For you math geniuses out there that means 90 degrees is set via a 1.5 millisecond high signal.

One problem is that these servos are stupid. One of their few jobs is to remember the length of that last pulse you sent them and they can’t do it. They forget how long that last high pulse was after 50 milliseconds or so. This means just telling a servo what what position to go to is not enough. You need to constantly remind it because it’s retarded (sorry the politically correct term is mentally challenged). This means the signal you send to a servo needs to look something like this:

Servo Waveform

Now that you understand the theory, it’s pretty easy to write some code for a microcontroller and I did just that for Arduino. My code was pretty dumb and it just waited around until it was time to send the high pulse. There are lots of ways you could try to improve this. Instead of waiting you could just check how much time has passed since the last pulse and send a new one if needed. Or you could get fancy with interrupts and/or timers. I decided to get lazy and just use someone else’s fancy code. There are a lot of libraries for Arduino that control servos. I picked the Software Servo Library (you can download it from this link). To install it you just put the downloaded servo folder in the arduino-0011\hardware\libraries directory.

The power provided by the Arduino board should be enough for a standard servo. If you want to control a lot of servos or a bigger one then you’ll need to wire in an external power supply. If you’re doing this make sure you connect the grounds for the Arduino board and your servo power supply or weird things will happen. Trust me I know. I spend an hour dreaming up ways of torturing my Arduino board into telling why the hell it was acting so strange. Finally I got a small servo and it worked fine because I used the Arduino board’s ground. Instantly I apologized to Arduino for all my mean words and now we’re great friends again!

Here is the circuit and a photo on the actual hardware I used.

Servo Circuit

Arduino and Servo

Last but not least is the code I wrote that uses the Software Servo Library. Here’s a link to the code. I’ve also listed it below.

// Maurice Ribble
// 4-6-2008
// http://www.glacialwanderer.com/hobbyrobotics

// This code depends on the Servo Software Library.  You can get
// this lib from http://www.arduino.cc/playground/ComponentLib/Servo

// To use this app just send the board a number followed by an 's'
// to set the angle of the servo (0-180 are valid)

#include <Servo.h>
// This is the pin you need to attach the servo to
#define SERVO_1_PIN 2

Servo g_servo1;

void setup()
{
  pinMode(SERVO_1_PIN, OUTPUT);
  g_servo1.attach(SERVO_1_PIN);
  Serial.begin(9600);
  Serial.println("Enter number and press 's' to send it to servo.");
}

void loop()
{
  static int val = 0;

  if (Serial.available())
  {
    char ch = Serial.read();

    switch(ch)
    {
      case '0'...'9':
        val = val * 10 + ch - '0';
        break;
      case 's':
        g_servo1.write(val);
        Serial.print("Servo1 is set to: ");
        Serial.print(val, DEC);
        Serial.println(" degrees");
        val = 0;
        break;
    }
  }

  Servo::refresh();  // Should try to call this every 20 ms to insure servo stays set
}

Comments (9)

SHT15 Humidity and Temperature Sensor

SHT15The first sensor I wrote a driver for on my new Arduino board was the SHT15. At this time both of these products are widely available at many internet stores and are relatively cheap ($30-$40 range). I’m very happy with the results I’ve gotten. It took me just a few hours to get everything working and it should take you even less time if you leverage the code below.

Below is the circuit diagram I have and a picture of the actual hardware. As you can see it’s extremely simple with just 4 wires needed for attaching the SHT15 to the Arduino board. I power the Arduino board off a USB port on my PC and I power the SHT15 from my Arduino board. Since both parts are very low power this works fine.

Circuit Diagram

Arduino Sht15

Here is a link to the code I wrote which has nice formating. Below is the identical code, but the formating might not be as nice for copying and pasting. To use this code you need to hook up your Arduino board to your PC and connect with HyperTermial or a similar tool that lets you communicate to your Arduino board. Once connected you just press “h” to take a humidity reading or “t” to take a temperature reading.

// Maurice Ribble 
// 4-3-2008
// http://www.glacialwanderer.com/hobbyrobotics

// This code tests the SHT15 on the Arduino board.
// This app assumes your data pin is pin 10 and your your clock pin is pin 11.
// To run this app just attach HyperTerminal (or similar serial communications tool)
// to your Arduino board at 9600 bits per second.  I also needed te set the
// flow control to none or I couldn't send commands to my Arduino board via HyperTerminal.
// Then press the 't' key to take a temperature reading or the 'h' key to take a
// humidity reading.

// These are commands we need to send to HSHT15 to control it
int gTempCmd  = 0b00000011;
int gHumidCmd = 0b00000101;
  
int shiftIn(int dataPin, int clockPin, int numBits)
{
   int ret = 0;
   int i;

   for (i=0; i 0)
  {
      cmd = Serial.read();
      
      switch (cmd)
      {
      case 't':  //Read Temperature
         {
           int val;
           int temp;
           
           sendCommandSHT(gTempCmd, theDataPin, theClockPin);
           waitForResultSHT(theDataPin);
           val = getData16SHT(theDataPin, theClockPin);
           skipCrcSHT(theDataPin, theClockPin);
           Serial.print("Temp is:");
           Serial.print(val, HEX);
           temp = -40.0 + 0.018 * (float)val;
           Serial.print("  ");
           Serial.println(temp, DEC);         
           break;
         }
      case 'h': //Read Humidity
         {
           int val;
           int humid;
           
           sendCommandSHT(gHumidCmd, theDataPin, theClockPin);
           waitForResultSHT(theDataPin);
           val = getData16SHT(theDataPin, theClockPin);
           skipCrcSHT(theDataPin, theClockPin);
           Serial.print("Humidity is:");
           Serial.print(val, HEX);
           humid = -4.0 + 0.0405 * val + -0.0000028 * val * val;
           Serial.print("  ");
           Serial.println(humid, DEC);
           break; 
         }
      default:
         {
           Serial.println("You pressed something else");
         }
      }
   }
}

Here is a page with more details about the SHT15.

Comments (23)

Site Update

Just for the record this site uses WordPress.  I also found I needed a way to make my code readable online and for that I’m using CodeHighlighter.

Comments off

Arduino

ArduinoThe advantage of a microcontroller over a PC for most of my projects is they offer an easy way to interface with different sensors and actuators. Most of the ports on a PC are for serial communication and just aren’t very good at talking to other devices that aren’t specifically designed to interface with USB, serial, or firewire ports. Other benefits are that microcontrollers use much less power and are cheaper than PCs.

In the past I’ve used a lot of different microcontrollers to control my various projects. On the high end of products I’ve tried was the TS-7400 which is pretty much a full blown PC, but instead of using an x86 processor it uses a more power efficient and slower ARM9 CPU. It ran any thing from a small embedded version of Linux to a full blown Debian distribution. It was a nice product, but it is overkill for most of my projects. When I need something like that most of the time I can just use a PC. It was a bit pricey too. These boards cost $130 each plus the up front cost of $200 for a development kit. It’s a nice system if it fits your need, but most of the time it didn’t fit mine.

What I usually need is a small microcontroller that easily interface with the wide variety of different sensors out there and with my PC. In the past I’ve used the PCC16F877. This worked nice but was a bit pricey and I needed to use their expensive CSS compiler. Not only was their compiler expensive, but I found a few bugs in it which wasted a lot of my time. I’ve talked about this combination here. Overall this system was nice, but I’ve recently found something better.

Just recently I got my first Arduino board. Let me say that this board is almost exactly what I want. It’s very easy to program. It’s open source and you can buy it from many stores for around $35. All the software to program it is open source and free so my total cost is $35 per board which is dirt cheap for what I get. About the only thing I wish I had for it was a good debugger, but for the smaller projects I’m doing using printf debugging is good enough. This board features tons of analog/digitial IO. It has pulse width modulation hardware for my servo projects. And because it’s open source it has a huge following and tons of online help for when I get stuck. Getting this platform up and working on my PC was extremely easy. I had their blinking LED project running in about 5 minutes. 20 minutes later I had written code where I used hyper terminal on my PC to talk to my arduino board and then Arduino talked back to my PC. Arduino is easy to use and has great online help. If you need a microcontroller for a project you should look at Arduino to see if it meets your needs.

I plan to use Arduino on many of my future projects. It rocks!

Comments (1)

Related Websites

I do a lot of tinkering with electronics, motors, PCs, sensors, micro controllers, and all those general DIY type projects.  I’m plan to post some of those activities to this blog.

Let’s get things started off by listing a few old websites I’ve managed to cover this same sort of information.

http://www.glacialwanderer.com/robots/

http://robotmaker.blogspot.com/

Comments off

« Previous Page « Previous Page Next entries »