there are many ways the code can go wrong however, if the sensor is working on the 5V from the analog in header, and it stops working the moment you move it to the 5V supply on P9.07 (without code changes) [which is what my understanding of what is happening to you], then there is definitely something weird, although I would be very surprised if the power management IC just decides to shut down or throttle the 5V supply, with the rest of the board being happy and healthy.
Can you try removing the audio expander capelet and see if the problem persists?
Another thing to try: run a regular program which uses audio and analog inputs. Plug in the two sensors between GND and 5V. If you read a voltage lower than 5V there, does that affect the audio and the analog inputs?
I also have another unorthodox thing to try: what about you just power the HC-SR04 from 3.3V? I know, the datasheet calls for 5V, however you have been successfully running it from 3.5V so far (because of the 100 ohm resistance in series with the output you were using), so it's worth trying if it works fine from 3.3V. When running from 3.3V, you may have to adjust (or remove) the resistor divider for the echo pin, but most likely it'll be fine . DO NOT run it without the resistor divider when you are powering (or trying to power it ) from 5V!