Search code examples
arduinooledbnanoarduino-ultra-sonic

Trying to get OLED to display input from ultrasonic range sensor using Arduino Nano


I am having a hard time getting the input from an ultrasonic range finder to display the distance on an Oled display. I am using an Arduino Nano. I can get the display to print Hello World while I can view on the Arduino IDE serial monitor all of the inputs from the range finder. I am using a 1.3 inch oled display and a 3 pin ultrasonic range finder. It has the vcc, ground, and signal pin. I have tried many different combinations to try and make it display but nothing is working. Here is what I have currently that at least makes both devices work at the same time. For the display and sensor the manufactures provided codes to make them work independently on the Arduino Nano. Sorry for all of the confusion with my code.

#include <U8glib.h>
#include "Arduino.h"

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // for 0.96” and 1.3”

class Ultrasonic
{
  public:
    Ultrasonic(int pin);
    void DistanceMeasure(void);
    long microsecondsToCentimeters(void);
    long microsecondsToInches(void);
  private:
    int _pin; //pin number of Arduino that is connected with SIG pin of Ultrasonic Ranger.
        long duration;  // the Pulse time received;
};
Ultrasonic::Ultrasonic(int pin)
{
  _pin = pin;
}
/*Begin the detection and get the pulse back signal*/
void Ultrasonic::DistanceMeasure(void)
{
  pinMode(_pin, OUTPUT);
  digitalWrite(_pin, LOW);
  delayMicroseconds(2);
  digitalWrite(_pin, HIGH);
  delayMicroseconds(5);
  digitalWrite(_pin,LOW);
  pinMode(_pin,INPUT);
  duration = pulseIn(_pin,HIGH);
}
/*The measured distance from the range 0 to 400 Centimeters*/
long Ultrasonic::microsecondsToCentimeters(void)
{
  return duration/29/2; 
}
/*The measured distance from the range 0 to 157 Inches*/
long Ultrasonic::microsecondsToInches(void)
{
  return duration/74/2; 
}

Ultrasonic ultrasonic(7);
void setup(void)
{
  Serial.begin(9600);

  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }
}
void loop(){
{
  long RangeInInches;
  long RangeInCentimeters;
  ultrasonic.DistanceMeasure(); // get the current signal time;
  RangeInInches = ultrasonic.microsecondsToInches(); //convert the time to inches;
  RangeInCentimeters = ultrasonic.microsecondsToCentimeters(); //convert the time to centimeters
  Serial.println("The distance to obstacles in front is: ");
  Serial.print(RangeInInches);//0~157 inches
  Serial.println(" inch");
  Serial.print(RangeInCentimeters);//0~400cm
  Serial.println(" cm");
  delay(100);
}
{
  // picture loop
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  // rebuild the picture after some delay
  delay(50);
}
}
void draw(void) {
  u8g.setFont(u8g_font_unifont);
  u8g.setPrintPos(5, 20); 
  u8g.print("Hello World!");
}

Solution

  • I can't try this out, but my guess is that you have to convert the range from the ultrasnoic sensor to a string, by using String() and then you can draw that on the OLED display. If you declare your variables outside the loop function you can use them in the draw function as well.

    long RangeInInches;
    long RangeInCentimeters;
    
    void loop() { 
        ...
        RangeInCentimeters = ...
        Serial.print(RangeInCentimeters);
    }
    
    void draw() {
        ...
        u8g.print(String(RangeInCentimeters));
    }