It may be caused by the external display, before I read your complete post my immediate reaction was a power supply issue.
Things to check is the external display, simply disconnect and then test to see if the issue continues.
To check the power supply you would need to take it to an AASP to have it checked out.
Finally to eliminate software issues, please post an EtreCheck report. If you are unfamilar with EtreCheck please read below:
Please navigate to the Mac App Store or navigate to www.Etrecheck.com and download the free version of EtreCheck. Once you have you downloaded the app and installed it, please run the report and save it. This report will help us get a good idea what has been installed on your system and help us be able to diagnose what may be wrong.
When you have your report, you can attach it when you reply to this message and we can then review it and help you determine what is needed to get your system running well again.
For instructions on how to download your EtreCheck report and attach it to your reply to this message please click https://discussions.apple.com/docs/DOC-250000211