Arduino pinMode(): When to use and why

All Arduino boards have GPIO pins with digital and analog capabilities. The Arduino pinMode() function determines how the pins will operate. A surprise might be that in some cases it is not necessary to use it. And when you do, pinMode() may not always work the way you expect. This post outlines how the Arduino pinMode() function works and when you should (or not should) use it.

pinMode(13, OUTPUT);

Continue Reading »

Sawtooth waveform with the Arduino Due’s Built-In DAC

Users of the ATmega328 based Arduino boards will recognize that the analogWrite() function doesn’t actually do anything analog.  It just sets the duty cycle of a pulse width modulated (PWM) signal.  The (new) Arduino Due board, which I have had limited time to play with, actually sports two Analog DAC channels.
Sawtooth Waveform
Using a very simple for() loop, it is possible to generate a sawtooth waveform using these channels.

Arduino: Fixing “Serial Port in Use”

Arduino Error: Serial port in Use

Here’s a couple of quick tips to get around the Arduino IDE error message “Serial Port in Use.”  These mostly apply to OSX, but since it is UNIX based, they may make sense for Linux as well.

List Open Files

Applies to:  OSX, Linux.

The check to see if another process (like a previous run of avrdude) is holding the serial port open is easy on both Linux and OSX.  Just open a terminal and run the command: lsof.  The command will report back all files that are currently open by the operating system.  UNIX-based operating systems do everything through files, so this command is useful to see what is keeping a serial device open.  Since lsof gives a list of everything that’s open, and there are a lot of “files”,  use grep to only list what is relevant.

Take a look at your Arduino IDE to see the device name assigned to your Arduino’s virtual serial port.  This name will change based on what USB to Serial chip and operating system you are using.  While you can use the full name like “/dev/tty.usbmodem023432” to search for the open device, I suggest just using a substring like “usbmodem“.

The format of the command is:

lsof | grep <device substring>

If nothing is returned, then that says no other processes are holding the file (or device handler) open.  If one (or more) processes are returned, you can do some further investigation.

James-MBP2b:~ james$ lsof | grep usbmodem
screen   68202 james   5u   CHR   33,16   0t3418   783 /dev/tty.usbmodem24131
James-MBP2b:~ james$

Here we can see that the process “screen” currently has the serial port open.  (Which I would expect, because I use screen in place of the Arduino Serial Monitor.)  Which, by the way, looks like this on OS X:

JavaAppli 6570 james   48u  CHR  33,16    0t562    783 /dev/tty.usbmodem24131

If you know the process shouldn’t have a hold on the device anymore, you can always make use of the kill command.

/var/lock

Applies to:  OSX.  (Maybe Linux)

If your installation of OSX is missing a writable lock file, you will get a Serial Port in Use error from avrdude, even though it isn’t.  So if you run the lsof command from above and don’t see anything holding the port hostage, then do this quick check to see if the lock file is writable (or exists):

ls -l /var/lock

If no file exists then run these two commands:

sudo mkdir /var/lock
sudo chmod 777 /var/lock

You’ll be asked for your system password the first time (and then it is remembered for the second) because of the sudo command.  You need temporary admin (root) privileges.  The mkdir comamnd creates a directory or folder while the chmod command will set permissions.  Feel free to click on the each of those links to verify what those commands do.

To be honest, I’m not entirely sure why this works.  Personally, I do not have a lock folder and avrdude connects to my Arduino boards just fine.  However, this simple fix has helped several people in the forums.  Since it is pretty innacious, it might be worth an attempt!

What if it is Still Broken?

These two fixes won’t always solve the Serial port issue.  If you still get the “Serial Port in Use” error even though you verified nothing else is using it, then you might want to post in the Arduino Forums for a bit more help.

Long Overdue LaunchPad Review

Recently I realized most of my projects were all Arduino-based.  There are lots of other prototyping platforms available in the market, and the TI LaunchPad, based on the MSP430 is one of them.  In fact, I have had one of these boards sitting in my box of stuff ever since they came out.  At $5, they because an impulse purchase when I was buying some other stuff.

So, I dusted off the original box and recorded opening it up.  Let’s see how it works.

Renesas Promotion Board for RL78/G13 | Renesas Electronics America

RL78 Kit from Renesis

Renesas is offering a promotion version of their RL78 microcontroller.  This offer is only available to their Sample Component account holders, but the process for signing up for a sample account is pretty straightforward.  (There may be EAR restrictions with this board.)

If you are looking for a powerful processor that is an alternative to PIC or AVR processors, this board might be worth a look.

[i]Note:  It appears samplecomponents.com might have been compromised at some point in the past.  Google throws a warning page up.  Proceed at your own discretion.[/i]

Renesas Promotion Board for RL78/G13 | Renesas Electronics America.

Arduino: Getting Real Time Help

While the Arduino Project Team provides their official Arduino forums, sometimes real-time help is just a bit better.  When that is the case, I encourage you to visit the freenode.net IRC channel, #arduino.  While as of today it is not an official channel, there are a number of great people available to help.

You can find me under the nick “baldengineer”, big surprise, I know.  Others like kline, Yoston and pwillard are also excellent people to ping with questions.

Arduino: Sending and Receiving Multi-Digit Integers

Arduino's Basic Serial Monitor

When Serial data is transmitted to an Arduino, it is sent one byte at a time.  Even though you might type “123” in the Serial Monitor, that’s not quite what is sent.  Instead the bytes “1” then “2” then “3” are sent.  Once received into a buffer on the Arduino, these individual bytes need to be reassembled into something useful.

Continue Reading »