Arduino Nano not sending & receiving data

I wanted to make a project in which there is an Ultrasonic Sensor, an OLED and a passive buzzer. I ordered those online, along with some wires and an Arduino Nano. When I had done the wiring, I opened Arduino IDE and started coding. As I had no knowledge about how an OLED display works, I searched it up on google.

After I had read a bit, I continued coding. When I plugged in my Nano in the computer, I wanted to specify the port, but it didn’t appear. There were only two ports: my bluetooth headphones and my microphone. I unplugged it and plugged it in again, the port only showing for a split second. I thought “maybe it’s the cable”, because I used an USB-Mini of another device. I went to my local electronics shop to ask for an Arduino Nano compatible wire, and I bought it.

With the new wire acquired, I plugged the Nano yet again in my computer. It was now shown, and it didn’t flicker anymore. With that problem solved, I uploaded the code to it, only showing the programmer not responding, not in sync, attempt x of 10. sort of thing. I changed the processor directory, and it worked.

Unfortunately, nothing worked as intended: the Ultrasonic oscilated between 5 and 1023 cm, with slight changes. The OLED screen didn’t light up, not even displaying the Hello World! I put in the void setup(). The buzzer didn’t make a single sound, even though I coded a function that allows it to play a frequency for some defined miliseconds.

I thought “maybe the wiring is wrong”, spoiler alert, it wasn’t. So I erased the entire program and put a simple digitalWrite(an led, HIGH); delay(500); digitalWrite(the same led, LOW); delay(500); and it worked. So I moved it to a PWM pin to see if the analogWrite() command worked. And it did. So I did the same thing to the Ultrasonic Sensor, but the serial monitor sent random distances. The same to the OLED, it didn’t work either. So I took a button, made it INPUT_PULLUP and checked the serial if when I pressed it the number from analogRead() worked. It… didn’t.

Can anybody tell me what can be wrong with that? (Yes, the built-in LED lights up when I press the reset button)

Your approach is wrong. Don't try to wire everything up at the beginning and then write the whole code. The chances of success are very low.

Begin with nothing wired up. Just the Arduino connected to the PC/laptop. Upload the example sketch called blink and get that running. Then test your other components, one by one, and choose an example sketch for each. This allows to you test that your components are not faulty, your wiring is correct and so on.

Once you have tested each component separately, start by choosing one component and a working example sketch. Making this your starting point, begin adding in the other components one by one, and building up your code a few lines at a time. Verify, upload and test frequently as you progress.

Thanks. Although I have some experience with Arduino Uno, and after I wire everything up it works almost perfectly on first try, I’ll try that. The problem still remains, though… Why isn’t it sending the right data?

Doubtful that they're "random". Maybe if you post your code (using code tags) we can have a looksee and help you get to the bottom of this.

I do not have the original code, but here is the button's:

const int button = 6;

void setup() {
  Serial.begin(9600);

  pinMode(button, INPUT_PULLUP);
}

void loop() {
  Serial.println(digitalRead(button));
}

and here's the buzzer's function:

int Beep(int BT) {
  tone(Buzzer, BT);
  delay(50);
  noTone(Buzzer);
}

The ultrasonic's was a variant I borrowed from one of my old projects:

digitalWrite(TrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(TrigPin, LOW);

  time = pulseIn(EchoPin, HIGH);
  dist = (0.034 * time) / 2;

And the OLED code I took it from https://arduinogetstarted.com/tutorials/arduino-oled, and it didn't work, although I haven't changed anything.

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-oled
 */

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width,  in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  Serial.begin(9600);

  // initialize OLED display with address 0x3C for 128x64
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while (true);
  }

  delay(2000);         // wait for initializing
  oled.clearDisplay(); // clear display

  oled.setTextSize(1);          // text size
  oled.setTextColor(WHITE);     // text color
  oled.setCursor(0, 10);        // position to display
  oled.println("Hello World!"); // text to display
  oled.display();               // show on OLED
}

void loop() {
}

The solution to this can be found in the IDE example sketch "Button" (I edited it to use INPUT_PULLUP:

/*
  Button
 
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. 
 
 
 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 2 from GND
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
 
 
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe
edited just now by Hallowed31 (for better of worse)
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
  // Pin 13: Arduino has an LED connected on pin 13
  // Pin 11: Teensy 2.0 has the LED on pin 11
  // Pin  6: Teensy++ 2.0 has the LED on pin 6
  // Pin 13: Teensy 3.0 has the LED on pin 13

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is LOW since we used INPUT_PULLUP:
  if (buttonState == LOW) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}

Try this: tested and works for me. Set serial to 115200 baud. One ultrasonic HC-SR04 (+5V, GND, Trig D6, Echo D7) and one pushbutton, (GND and D2). Press button to activate sonar. Prints distance while button held and either "near" or "far" depending on object detection range.
Sonar stuff requires the great NewPing library by Tim Eckel and I always use and adapt the many sensors example even if I'm just using one. Code below should give you an idea of where to add in your own stuff and how to use a button as a wrapper if you only want to use the sonar part of the time.

#include <NewPing.h>

const int buttonPin = 2;
const int trig1 = 6;
const int echo1 = 7;
const int sonarNum = 1;
const int maxD = 400; // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400cm.
const int pingInterval = 33; //milliseconds in between pings
unsigned long pingTimer[sonarNum]; // When each pings.
unsigned int cm[sonarNum]; // Store ping distances.
uint8_t currentSensor = 0; // Which sensor is active.

NewPing sonar[sonarNum] = { // Sensor object array.
  NewPing(trig1, echo1, maxD),
};

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
 int buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    for (uint8_t i = 0; i < sonarNum; i++) {
      currentSensor = i;
      cm[currentSensor] = 0;
      sonar[currentSensor].ping_timer(echoCheck);
      oneSensorCycleCloseRange();
    }
  }
}

void oneSensorCycleCloseRange() {
  unsigned int sensor1 = cm[0];
  printSonarDebugInfo();

  if ((sensor1 >= 10) && (sensor1 <= 20))
  {
    Serial.println("near");
  }

  else if ((sensor1 >= 25) && (sensor1 < 40))
  {
    Serial.println("far");
  }

  else {}
}

void printSonarDebugInfo() {
  Serial.println(); // better readability
  for (uint8_t i = 0; i < sonarNum; i++) {
   // Serial.print(i + 1);
   // Serial.print("=");
    Serial.print(cm[i]);
    Serial.println("cm ");
  }
  Serial.println(); // also better readability
}

void echoCheck() { // If ping received, set distance to array.
  if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}



library:
https://www.arduino.cc/reference/en/libraries/newping/

I’ll try this. Thanks for your help!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.