A while back, I built a small solution to measure my 5t rainwater tank. It's a esp8266 running esphome with a waterproof SR04 attached. I'm currently only measuring the first tank as it's near a powerplug. The next tank is about 7m further away. I won't be able to reach it with the SR04 cable (~2m). For a different project, I started experimenting with using Cat6 cables to extend sensor cables (DS18b20 in the other case) which worked really well.
For the SR04, I thought it'd be easiest to mount a small box with the small eletronics board close to the tank and then run the two signal + 5v + gnd back to the main esp8266 by the first tank. I connected each of the twisted pairs to one of the four needed lines (trigger/echo/5v/gnd). For testing, I'm using a Wemos D1 Mini, powered via usb. Now to the problem:
When I connect the wires and start up the Mini, nothing happens. When I start it without the 5v connected (side note, same behavior on the 3.3v pin) connected, it boots up and gives 0cm measurements. If I then plug the 5v in, the measurements are correct.
I'm not particularly knowledgable about electronics. My very amateur reasoning was that the long cable creates a higher-than-spec amp draw which kills the Wemos. Is that correct? What can I do to prevent that? My currently only plan would be to put the 5v behind a mosfet and switch it 'live' only after a few ms after boot.
Without a schematic of your setup (please, not a fritzing picture), we would be just guessing. How are you getting 5V to the sensor? What is the voltage you measure at the sensor?
Hi - my apologies. here's the very crude schematic for now, I need to measure voltage later today. It'll be at least over 3.3v on the sr04 side as the measurements are correct.
The blue light blinks once at start up. My first debug message right at the start doesn't show. The serial monitor is just blank.
Using my DMM, I measure 4.6V at the far end when using the 5V pin on the Wemos and 3.3V when using the 3.3V pin. Both have the same issue - the wemos crashes when starting up with the cat6 cable (specifically, the 3/5V wire) plugged in. If I plug in the 3/5V wire AFTER start-up, the wemos & sensor work fine - producing the right measurements.
I would try powering the SR04 from a separate power source (be sure the grounds are connected together) to rule out a power supply issue.
Also, how do you know it's rebooting? Put a Serial.print() in the setup function. If it really reboots then you will see the Serial.print() twice. If you don't want to use a serial monitor, then put an LED on an unused pin and light it during setup().
What voltage is the 5V pin of the wemos? If there is a voltage drop along the cable when using 5V, then there should be a similar drop when using 3.3V, but you are not seeing that.
The 5v pin actually only spits out 4.6V, so there's essentially no voltage drop across the 7m cable.
I then changed the code to add a 5s delay in setup, to make sure the setup messages show before starting the loop. Whenever the wemos 'crashes', no messages show. I'm fairly certainly it's crashed/doesn't boot.
I then kept the ech/trg wires attached via the 7m cable, and used short jumper cables to run 5v + gnd directly from the wemos. Same issue! It still crashed. As before, if I boot without the 5v attached, it starts. I can then add the 5v wire and the measurements are correct. Interestingly, if I boot with 5v + gnd (via short jumper wires) but WITHOUT the echo/trg (7m cable), it also boots ok. I can then attach these two and the measurements start.
I don't think the code is the issue, I left out all the boring bits (headers etc) at the top if that's what you mean. In any case, i've edited the post above for completeness sake.
Only a couple of lines, I'm not sure why you bothered removing them. I wanted to check the pin numbers used in the code matched your diagram. Sometimes beginners get confused between the esp pin numbering system, eg. 'GP02' or just '2' versus the wemos/nodemcu pin numbering system, eg. 'D4'.
Your problem seemed to have the hallmarks of something pulling down the D3 or D4 pins, or pulling up the D8 pin, preventing the wemos from booting and running the sketch, for example by putting it into firmware upload mode. But those pins don't seem to be connected to anything, are they? When the cable is connected and the wemos refuses to boot, what are the voltages on those pins?
Bit of background why I left it out: I've been using several ultrasonics with both nodemcus and wemos d1s for rainwater and septic tanks for over a year. I get your point re pins - totally fell for that in the beginning. The code I used for testing had a whole bunch of extra (deep sleep, WiFi, mqtt, voltage measurements etc). Which wasn't relevant. To be sure, I did run everything again with the posted minimal version above. Same results.
I also tried different combinations of pins, currently just using d5/6 as that's my default. Nothing else other than gnd/5v and USB is connected.
All works 100% with the same code and pins if I use jumper leads. As soon as I replace the jumper leads with the cat6, it crashes unless I connect it after boot.
I'm a bit confused. I get about 20mA with the jumper cables - which is close enough to spec (15mA). With the Cat6 cable, I get 0.6mA - yet the measurements are still correct - something probably isn't right.