The use of the ESP8266 in the world of IoT

User avatar
By dperezq
#35377 I'm trying to manage a HC-SR4 sensor with the ESP8266-201. I got to upload the code and show the distance gathered from the HC, but after a while it shown an error and the console stops. I am starting with the ESP so I have no idea why this error. The code is build with Arduino IDE and the output of the console is:

Code: Select all50in, 128cm
49in, 126cm
49in, 126cm
23in, 59cm
23in, 58cm
23in, 59cm

Exception (0):
epc1=0x40216938 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ctx: sys
sp: 3ffffd00 end: 3fffffb0 offset: 01a0

>>>stack>>>
3ffffea0:  00000140 4021a2ba 00000000 3fff0300 
3ffffeb0:  ffffffca 3ffef3a8 40103797 3ffeda90 
3ffffec0:  00000000 00000000 00000020 40103687 
3ffffed0:  00000000 00110b0b 00640000 00000070 
3ffffee0:  3ffec320 0000011c 3ffec360 3ffec314 
3ffffef0:  3ffec344 3ffec320 3ffec331 3ffec33e 
3fffff00:  00000000 00000000 00000000 00000000 
3fffff10:  00000000 00000000 00000000 00000000 
3fffff20:  0000002a 00000000 3fff06b8 40219ec2 
3fffff30:  3ffedb80 3fff0300 00000000 3ffef400 
3fffff40:  3ffedb80 00000000 3ffec2f0 3ffec2fc 
3fffff50:  3ffec2fc 00000144 00000000 0000002a 
3fffff60:  00000000 3ffec43c 4020a2c9 3ffedb80 
3fffff70:  3ffec2f0 000000f5 3ffea168 3ffea168 
3fffff80:  00000080 3ffedb80 00000004 3fffdcb0 
3fffff90:  40209c74 3fffdab0 00000004 3fffdcc0 
3fffffa0:  3ffea168 40000f49 3fffdab0 40000f49 
<<<stack<<<

 ets Jan  8 2013,rst cause:1, boot mode:(1,7)


 ets Jan  8 2013,rst cause:4, boot mode:(1,7)

wdt reset


I am powering the sensor with 5V and the ESP with 3.3V
User avatar
By dperezq
#35882 This is the code:

Code: Select allconst int trigPin = 5;
const int echoPin = 2;

void setup() {
  // initialize serial communication:
  Serial.begin(115200);
}

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, cm;

  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(1000);//1000msec = 1sec
  //5min = 1000*60*5
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
User avatar
By picstart
#35899 I've worked with the HC-SR04 only with PIC MCU's. I always used an interrupt and a timer to detect the rising pulse and the falling pulse.
I'm not sure how pulsein is implemented in the Arduino esp IDE. The esp8266 has WiFi to deal with and has to respond asynchronously. Delays are written to be non-blocking to give the esp8266 cycles to maintain WiFI. Perhaps pulsein is blocking and denies, every now and then, the esp8266 what it needs in terms of cpu cycles.
Perhaps using an esp pin as an interrupt and a timer would avoid the issue.