0

So, i need to send sound sensor data from an Arduino Uno to the ESP8266 (ESP-01), so it can output a push notification to my phone whenever there's a loud noise. I have the sensor just powered and hooked up to the Analog 0 pin, and the ESP powered and hooked up to the Arduino Serial pins in reverse (RX to TX, TX to RX)

This is the code i have on the Arduino:

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.write(analogRead(A0) + "\r\n");
  delay(100);
}

And this is the code i have on the ESP:
(i'm omitting the notification part for clarity, it doesn't even use Serial, so i don't think it should interfere)

void setup() {
  Serial.begin(115200);
}
void loop() {
  String val;
  val = Serial.readString();
  if( val.toInt() < 50 ){ sendNotification("Hello!"); delay(10000);}
}

I have tested the Sensor output and it seems fine, whenever i snap or clap the readings show a peak (although reversed, it stays on 1023 constantly then peaks to around 0~50), and the ESP can also send notifications normally. But the trigger on the notification never seems to fire, so i guess the ESP is not receiving the data properly.

I'm new-ish to Arduino, so i don't know if i'm doing something wrong here, Thanks in advance!

  • Serial.println(analogRead(A0)); will send the value as text terminated by \r\n. at receive side use at least readStringUntil – Juraj Nov 21 at 15:19
2

Writing Junk From Memory

Serial.write(analogRead(A0) + "\r\n");

This is advancing a pointer to "\r\n" by whatever number is being returned by analogRead. In other words, you are not sending the measurement but junk from memory after (or sometimes at) "\r\n"'s memory location.

Serial.println(analogRead(A0)); appears to be what you were going for here. println's line termination for the UNO serial connection is "\r\n".

Logic Levels

When it comes to attaching the serial connections together, there should be some level conversion going on. Because an UNO's pins are 5V logic levels and the ESP-01 expects 3.3v levels.

| improve this answer | |
  • Thanks for the info! Just writing the analogRead(A0) wasn't working, so i supposed the ESP needed the newline and carriage to accept input, just like the AT commands. The level conversion you said is an hardware conversion, like reducing the 5V to 3.3V with a resistor, or is it a software conversion of some kind? – Jao Nov 21 at 15:20
  • For the UNO TX to ESP-01 RX a resistor voltage divider will probably work fine. Going in the other direction you'll need some kind of buffer to reliably get to the UNO's minimum logical level high voltage. – timemage Nov 21 at 15:26
  • Thanks again! I'll go buy a regulator and see if it works. Another question, i only need to send the serial from the UNO to the ESP, the ESP never sends anything back. Do i need the buffer to regulate the tension for the ESP to UNO connection anyway? – Jao Nov 21 at 15:32
  • @Jao: there are things called "logic level converters" available that are built for this sort of scenario. Worth a look instead I think. – Mat Nov 21 at 15:40
  • @Jao, in the direction of UNO to ESP it is likely you just need to create a divider on your UNO TX's signal and the level converter would not be strictly necessary, although one can probably be more efficient. A divider is a simple solution. Keep in mind that your UNO TX/D1 is the same signal ultimately goes back to the PC, so when you use it to send to the ESP-01 you are also sending stuff to the PC/Serial Monitor. – timemage Nov 21 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.