Ultrasonic display on OLED128

128x128 pixel 1.5" full colour OLED display with MicroSD card slot. [Product page]
Post Reply
PaulC
Posts: 4
Joined: Wed May 06, 2015 4:06 am

Ultrasonic display on OLED128

Post by PaulC » Wed May 06, 2015 4:50 am

Hey all,

I've been experimenting with using an SR04 ultrasonic sensor and having the OLED display the distance.
Eventually I hope to have the OLED display several sensor readings from ultrasonic, barometric, temp etc.

So far I have been able to get the OLED to display text on the screen but not the distance measurement from the sensor.
https://www.dropbox.com/s/69zsuhc7utvlr ... 1.jpg?dl=0
The Echo and Trigger are hooked up to pins 10 and 12 (Serial readout works with the NewPingExample sketch.

However when the OLED is wired up as you can see, there is no change in what is displayed.
https://www.dropbox.com/s/5io40j4y89spe ... D.jpg?dl=0

Here is the code so far
//Ultrasonic sensor display on OLED

#include <SPI.h>
#include <SD.h>
#include <FTOLED.h>
#include <fonts/SystemFont5x7.h>
#include <NewPing.h>
#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     10  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
const byte pin_cs = 7;
const byte pin_dc = 2;
const byte pin_reset = 3;
OLED oled(pin_cs, pin_dc, pin_reset);
OLED_TextBox top(oled, 0, 64, 128, 64);
OLED_TextBox bottom(oled, 10, 54, 128, 64);
void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  oled.begin();
  oled.selectFont(SystemFont5x7);
  top.setForegroundColour(RED);
  top.setBackgroundColour(BLACK);
  top.println("RANGE");
}

void loop() {
  delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  oled.selectFont(SystemFont5x7);
  bottom.setForegroundColour(GREEN);
  bottom.setBackgroundColour(BLACK);
  bottom.print("Distance: ");
  bottom.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
  bottom.println("cm");
  bottom.reset();
}
Any help would be greatly appreciated
I hope it isn't something simple that is obvious hehe

Thanks guys

Paul C

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: Ultrasonic display on OLED128

Post by angusgr » Wed May 06, 2015 11:34 pm

Hi Paul,

I can't quite read the output in the second photo, is it still zero being displayed there?

One thing that may help is to put some Serial.print statements in the loop, so you print the output as well as displaying it on the OLED. You could try printing the raw uS value as well.


Angus

PaulC
Posts: 4
Joined: Wed May 06, 2015 4:06 am

Re: Ultrasonic display on OLED128

Post by PaulC » Thu May 07, 2015 1:45 am

Hey Angus

Thanks for the reply.

Yes the zero output is the same in both photos.

When I add serial.print to the sketch, 0cm is displayed in the serial monitor so the issue remains.

I have tried this modification but no joy.
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
oled.selectFont(SystemFont5x7);
bottom.setForegroundColour(GREEN);
bottom.setBackgroundColour(BLACK);
bottom.print("Distance: ");
bottom.print(uS); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
bottom.println("cm");
bottom.reset();
}
Also I have managed to set up the Freetronics Humidity/Temp sensor to the OLED
and managed to get it working first time around. I was able to use sections of my ultrasonic
sketch and incorporate it into the DHT example sketch. (I will detail this in a separate post)

Side question: Does the Baud rate have anything to do with the OLED? The ultrasonic sketch uses 115200 for the serial monitor whereas the DHT only uses 9600.

Thanks

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: Ultrasonic display on OLED128

Post by angusgr » Thu May 07, 2015 1:48 am

Hi Paul,

No, either baud rate should be fine. It seems like the OLED is working OK, the problem seems to be that the NewPing library is reporting 0 for the ping time?

Does the NewPing library work if you take all of the OLED stuff out?

Angus

PaulC
Posts: 4
Joined: Wed May 06, 2015 4:06 am

Re: Ultrasonic display on OLED128

Post by PaulC » Thu May 07, 2015 1:51 am

Yeah mate the default Newpingexample sketch works fine reading to the serial monitor. Things go wrong when the OLED is involved.

This is doing my head in lol

PaulC
Posts: 4
Joined: Wed May 06, 2015 4:06 am

Re: Ultrasonic display on OLED128

Post by PaulC » Thu May 07, 2015 6:24 am

Well now the ultrasonic sensor suddenly started working :? I'm not sure how but it may have something to do with what pins I chose.

Here is a photo of the full setup showing the humidity/Temp, Light and Ultrasonic sensors all working and displayed on the OLED.
https://www.dropbox.com/s/gvcr4m1h8ot6h ... 1.jpg?dl=0

Arduino pinouts are as follows:
Sensors
Ultrasonic
Echo--8
Trigger--9
Photocell
Output--A0
Humidity/Temp
Signal--1
OLED (As per the second example on Freetronics website.)
1--Vin
2--Gnd
6 --11
7--13
8--7
9--2
10--3

Here is the sketch I have made so far.
/*Multisensor readout to OLED display by Paul Coller
Arduino Eleven from Freetronics running Freetronics Humidity/Temperature sensor, light sensor and generic ultrasonic sensor(HC-SR04)
with readings displayed to Freetronics OLED display.
Modified from original sketch for various DHT humidity/temperature sensors that was
written by ladyada.
All coding in public domain.
*/
#include <SPI.h>
#include <SD.h>
#include <FTOLED.h>
#include <fonts/SystemFont5x7.h>
#include "DHT.h"

#define DHTPIN 1 // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);
const byte pin_cs = 7;
const byte pin_dc = 2;
const byte pin_reset = 3;
OLED oled(pin_cs, pin_dc, pin_reset);
OLED_TextBox line1(oled, 0, 64, 128, 64); //Text box for title
OLED_TextBox line2(oled, 0, 54, 128, 64); //Text box for humidity reading
OLED_TextBox line3(oled, 0, 44, 128, 64); //Text box for the temperature reading
OLED_TextBox line4(oled, 0, 34, 128, 64); //Text box for the light level reading
OLED_TextBox line5(oled, 0, 24, 128, 64); //Text box for the distance reading
int photocellPin = 0; //Analog pin for the photocell
int photocellReading;
#include <NewPing.h>
#define TRIGGER_PIN 9 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 8 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
dht.begin();
oled.begin();
oled.selectFont(SystemFont5x7);
line1.setForegroundColour(BLUE);
line1.setBackgroundColour(BLACK);
line1.println("Environmental Status");//Set the title
}

void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();

// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
oled.selectFont(SystemFont5x7);
line2.setForegroundColour(RED);
line2.setBackgroundColour(BLACK);
line2.println("Sensor Error"); //Displays if sensor not connected
delay(500);
line2.reset();
line3.setForegroundColour(RED);
line3.setBackgroundColour(BLACK);
line3.println("Sensor Error"); //Displays if sensor not connected
delay(500);
line3.reset();
} else {
oled.selectFont(SystemFont5x7);
line2.setForegroundColour(GREEN);
line2.setBackgroundColour(BLACK);
line2.print("Humidity: ");
line2.print(h);
line2.print(" %\t");
line2.reset();
oled.selectFont(SystemFont5x7);
line3.setForegroundColour(YELLOW);
line3.setBackgroundColour(BLACK);
line3.print("Temperature: ");
line3.print(t);
line3.println(" *C");
line3.reset();
}
{
photocellReading = analogRead(photocellPin);
Serial.print(photocellReading); // the raw analog reading, displayed to the serial monitor. Change to line4.print if required.

// We'll have a few threshholds, qualitatively determined
if (photocellReading < 10) {
oled.selectFont(SystemFont5x7);
line4.setForegroundColour(ORANGE);
line4.setBackgroundColour(BLACK);
line4.print("Light level:");
line4.println("-Dark");
line4.reset();
} else if (photocellReading < 200) {
oled.selectFont(SystemFont5x7);
line4.setForegroundColour(ORANGE);
line4.setBackgroundColour(BLACK);
line4.print("Light level:");
line4.println("-Dim");
line4.reset();
} else if (photocellReading < 500) {
oled.selectFont(SystemFont5x7);
line4.setForegroundColour(ORANGE);
line4.setBackgroundColour(BLACK);
line4.print("Light level:");
line4.println("-Light");
line4.reset();
} else if (photocellReading < 800) {
oled.selectFont(SystemFont5x7);
line4.setForegroundColour(ORANGE);
line4.setBackgroundColour(BLACK);
line4.print("Light level:");
line4.println("-Bright");
line4.reset();
} else {
oled.selectFont(SystemFont5x7);
line4.setForegroundColour(ORANGE);
line4.setBackgroundColour(BLACK);
line4.print("Light level:");
line4.println("-V bright"); //Changed from "-Very bright" to "-V bright" do to limited space on display.
line4.reset();
}
delay(200);
}
{
delay(50);
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
oled.selectFont(SystemFont5x7);
line5.setForegroundColour(PURPLE);
line5.setBackgroundColour(BLACK);
line5.print("Distance: ");
line5.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
line5.println("cm");
line5.reset();
}
}
Eventually I hope to include more sensors and then mount this all in a box as a sortof "Tricorder"

*Note: I had to shorten one of the light level values because it displayed on the the next line and wouldn't reset when the value changed on the OLED.
I am not sure how to fix this issue.

Cheers

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: Ultrasonic display on OLED128

Post by angusgr » Tue May 12, 2015 10:44 pm

Hi Paul,

Glad you got it working.
PaulC wrote:*Note: I had to shorten one of the light level values because it displayed on the the next line and wouldn't reset when the value changed on the OLED.
I am not sure how to fix this issue.
The OLED_TextBoxes are designed to line wrap text and scroll within their size. Because all the boxes are defined as 64 pixels high, they'll automatically wrap onto the next line.

Calling box.reset() only moves the cursor back to the top, it doesn't erase the contents of the box. Calling box.clear() will erase the contents of the box (so it'll clear the second line), but it will also look flickery on the display.

As you only want to draw one line at a time, you might find it easier to use the drawString() method on the OLED itself. This doesn't do any line wrapping or scrolling, it just draws a string.

So instead of:

Code: Select all

line3.print("Temperature: ");
line3.print(t);
line3.println(" *C");
line3.reset();
Something like:

Code: Select all

String line = F("Temperature: ");
line += t;
line += F(" *C   "); // note the extra spaces on the end here, a hack to make sure we blank out any previous longer line
oled.drawString(0, 44, line, YELLOW, BLACK);

line = F("Light level:");
// .. etc, etc.
The FTOLED wiki has some more information about drawString: https://github.com/freetronics/FTOLED/w ... ic-strings

Angus

csconsulting
Posts: 62
Joined: Fri Sep 21, 2012 7:22 am

Re: Ultrasonic display on OLED128

Post by csconsulting » Thu May 28, 2015 11:01 am

char deg[3];

deg[0]=176;
deg[1]=67;
deg[2]=0; // this is for the degree symbol, and the C


This provides for you the little degree symbol and a capital C for your temperature readout, if the font is fully ascii compliant.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest