Hi, I see you are having an issue with device connectivity. No worries, let's get this fixed!
There might be a possibility you are not using a correct cord. This may cause connection issues between your Mac and the device you are attempting to connect.
If you are using an adapter, check to see if it is an official Apple adapter.
In terms of it happening when your MacBook goes to sleep, it may be shutting off any disks when this happens.
Try keeping your MacBook on when the device is connected. If you are trying to get items from the hard drive onto your Mac or vice versa, you may have to keep an eye on your MacBook while the process is being completed.
Another issue that may cause this is an improperly connected cord. Your cord may not be fully plugged in. Try making sure your cord is fully connected to both the Mac and the hard drive. There may also be foreign objects (dust, lint, dirt, grime, etc.) in the ports. Try cleaning the ports on your MacBook and the hard drive. Do not use water to do this (Yes, people have done this before, yes, it will damage your MacBook). Use compressed air or a toothpick. Do not use a paperclip or anything metal as this may cause a short-circuit and damage components.
A final cause may be the hard drive itself. The hard drive may be incompatible, damaged, or otherwise refusing to connect. Try seeing if your Mac is compatible with the device. It is unlikely as Apple tries to maximize compatibility with external accessories, but sometimes some slip through the cracks and the Mac cannot recognize it.
I hope this all helps!