0 votes
in ESP8266 OLED Starter Kit by (120 points)
#include <Wire.h>
#include <SPI.h>
#include <NewPing.h>
#include "SH1106.h"
#include "SH1106Ui.h"
#include "Ultrasonic.h"
#define OLED_RESET  D1   // RESET
#define OLED_DC     D2   // Data/Command
#define OLED_CS     D8   // Chip select

// Pin definitions for SPI
// ESP8266
//#define OLED_RESET  5   // RESET
//#define OLED_DC     4   // Data/Command
//#define OLED_CS     15  // Chip select
// Node MCU

SH1106 display(true, OLED_RESET, OLED_DC, OLED_CS); // FOR SPI
SH1106Ui ui  ( &display );

#define TRIGGER_PIN  D3  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     D4  // 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.
#define SensorPin A0            //pH meter Analog output to Arduino Analog Input 2
#define Offset 0.00            //deviation compensate
#define LED D1
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth  40    //times of collection
int pHArray[ArrayLenth];   //Store the average value of the sensor feedback
int pHArrayIndex=0;  
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.


void setup() {
  Serial.begin(74880); // Open serial monitor at 115200 baud to see ping results.
  Serial.println("pH meter experiment!");    //Test the serial monitor


void ultrasonic() {
  delay(1000);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)

void ph()
  static unsigned long samplingTime = millis();
  static unsigned long printTime = millis();
  static float pHValue,voltage;
  if(millis()-samplingTime > samplingInterval)
      voltage = avergearray(pHArray, ArrayLenth)*5.0/1024;
      pHValue = 3.5*voltage+Offset;
  if(millis() - printTime > printInterval)   //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
 // Serial.print("Voltage:");
        Serial.print("    pH value: ");
double avergearray(int* arr, int number){
  int i;
  int max,min;
  double avg;
  long amount=0;
    Serial.println("Error number for the array to avraging!/n");
    return 0;
  if(number<5){   //less than 5, calculated directly statistics
    avg = amount/number;
    return avg;
      min = arr[0];max=arr[1];
        amount+=min;        //arr<min
      }else {
          amount+=max;    //arr>max
          amount+=arr[i]; //min<=arr<=max
    avg = (double)amount/(number-2);
  return avg;

void loop() {
  int remainingTimeBudget = ui.update();
  if (remainingTimeBudget > 0) {

void loop () {

somente no monitor serial

mas não no display, alguém pode ajudar?

1 Answer

0 votes
by (3.6k points)
Qual tipo de prancha? Wemos D1 Mini ou algum outro tipo?

Qual tipo de exibição? 1.3 "OLED com barramento I2C?

Quase certamente suas conexões de pinos estão erradas.

Welcome to ThingPulse Q&A, where you can ask questions and receive answers from other members of the community.