Top Stories /

Off-The-Shelf Hacker: If One Gauge Is Good, More Are Definitely Better

28 Mar 2018 12:00pm, by

I’ve been on an onscreen gauge kick lately. The steampunk conference badge has an ultrasonic rangefinder that displays the distance to an object via an analog gauge on the front-mounted 3.5-inch LCD screen. The steampunk lettering and embellishments on the gauge face compliment the overall theme of the badge.

Well, if one gauge is good, two must certainly be better. Who knows, you might have a project where you want to track speed, temperature, light levels and other such things, all at once on some kind of stylized dashboard.

I want to use one gauge to display the rangefinder data and one gauge to measure the ambient light falling on a photocell. The photocell will be one of several on the side of the badge that I’ll use instead of push buttons. Cover the photocell with a finger, and it effectively pushes a button. Showing the light level on a gauge is a way to test the operation of the “photo push button” and learn about using multiple gauges.

We’ll cover the Arduino part today. Next week we’ll go over the mods on the Raspberry Pi Processing gauge side.

Update the Pro Mini Code for Two Data Streams

Operation of the single ultrasonic sensor is straightforward.

We read ranging data from the sensor using an Arduino Pro Mini then turn the data into a text stream. The text stream is then sent over the serial or USB line to a Processing script on a Linux computer. The script displays the data on an analog gauge. The wired serial line is used during portable conference badge operation. A USB connection is used during development and programming.

Since we want to display the values from both an ultrasonic range finder and a photocell, we’ll need to send two sets of data across the USB line. Each additional gauge would need a new data stream.

I’m sad to report that I just didn’t quite get around to actually soldering the photocell into the badge, yet. Projects always take longer, and life’s challenges sometimes conspire against you. Not to worry though, because we can use an old trick I learned back in the Big One, WWI.

That’s right… a simulation.

Seriously, there’s no need to hold up the project just because we don’t have a “real” sensor in place. Simulating the data is pretty easy. A nice little technique to remember for future projects.

The baseline code came from the HC-SR04 ultrasonic rangefinder program in the Arduino examples. I simply added a few lines to increment a second data value from 0 to 100 and put that in the serial print line of code to be sent out over USB. When the data value hits 100, it resets to 0 and does that over and over. Looking at the onscreen gauge, we’ll see the pointer slowly sweep up to 100 then jump back to 0 and repeat. When I get around to wiring in the actual photocell, I’ll change the code to read the analog pin attached to the photocell and post the value in place of the incremented simulated value. How easy is that?

/*
[HC-SR04 Ping distance sensor]
VCC to arduino 5v; GND to arduino GND
Echo to Arduino pin 7; Trig to Arduino pin 8
*/

#include 

Servo myservo;  // create servo object to control a servo

#define echoPin 8
#define trigPin 7
int val, g;

void setup() {
  Serial.begin (115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  myservo.attach(9);
}

void loop() {
  // send a pulse on the trigger pin to initiate measurement
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // the length of the pulse on the echo pin is proportional to the distance
  long duration = pulseIn(echoPin, HIGH);
  long distance = (duration / 2) / 29.1 * .3937 ;

  if (distance >= 200 || distance <= 0) {
    // Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.print(",");
    if (g == 100) {
       g = 1;
         }
    else {
       g++;
       }
    Serial.println(g);
    // Serial.println(" in");
  }
  
  val = map(distance, 1, 70, 0, 180);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);
  // Serial.println(val);
  
  delay(300);
}

It gets even better because to edit the code on the Pro Mini, we can load the Arduino IDE on the Raspberry Pi and make the modifications right there.

Making It Better

In past articles, I’ve used my Linux notebook with the latest Arduino IDE to program the Pro Mini. An FTDI serial-to-USB cable works great with this particular Arduino. Just plug it into the onboard connectors and the other end into the USB port and you’re ready to rock. The power switch for the Pro Mini needs to be in the off position, so there is no conflict with the regular wired serial connection to the Raspberry Pi.

There’s no need for an external notebook if you use a Raspberry Pi with additional microcontroller boards like the Arduino. Hook up an HDMI monitor and external keyboard/mousepad to do all the coding and development on the Pi. Instead of hooking the Pro Mini cable up to the Linux notebook, just plug it into one of the available USB ports on the Pi.

The conference badge has a Raspberry Pi 2 model B and it’s definitely fast enough to use both the Arduino or Processing IDE applications.

To install the Arduino IDE, open a browser on the Raspberry Pi and download the latest version from the Arduino web page.

Once the tar file is downloaded, expand the file with the following command.

tar xvf arduino-1.8.5-linuxarm.tar.xz

Don’t forget to download the ARM edition of the file. Version 1.8.5 was the latest at the time of this article. Also, make sure your Pro Mini is connected to the serial-to-USB cable and then change directories into the Arduino directory.

You will probably want to set the serial port with the stty command, too.

stty -raw -icrnl -F /dev/ttyAMA0 115200

Start the Arduino IDE with the following command.

./arduino

What’s Next

Now that we’ve seen the changes needed on the Arduino side of the badge, the next step is to mod the code on the Raspberry Pi. We’ll take the data stream with two values and break it up so they can go to their respective gauges. That will be next week’s topic.

As our projects become more sophisticated, so do our programming techniques and the number of tasks we need to perform to make things work. The badge is pretty simple with a single Pro Mini attached to a single Raspberry Pi.

What kinds of projects might use more than one Arduino or more than a few sensors, feeding a Raspberry Pi? Can you think of any jobs that might need more than one Pi?

If one is good, several could well be awesome.


A digest of the week’s most important stories & analyses.

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.