This question already has an answer here:
Sometimes I activate the Protectron in Corvega Assembly plant, go to the side of the room and sneak as I watch it attack raiders. Then, all of a sudden, it would turn and start attacking me. Sure, sometimes I had tried to help it by firing a few shots, and maybe it saw that as being bad, but other times during the entire duration of it being active I don't use any weapons and just try to hide off to the side.
The strange part is it doesn't always happen, either. There are good odds it will not attack me unless I attack it first, but sometimes it seems to just decide it doesn't like me and attacks. Why is does it sometimes attack me?