Yes, the First Amendment protects our freedom of speech. But there are still things you can't say. Obviously threatening to kill someone, especially the president, is a no-no, and as Justice Oliver Wendell Holmes famously wrote, "The most stringent protection of free speech would not protect a man in falsely shouting fire in a theater and causing a panic."
So how are free speech rights balanced when it comes to interacting with police? Given the heightened tensions between officers and civilians, an increase in interactions at protests and demonstrations, and a rise in awareness and curiosity about legal rights when coming in contact with cops, exactly what you can, and can't, say to police officers has become a hot topic. Here are your general boundaries when it comes to swearing or yelling at police officers:
Normally speaking, it's not the volume of your voice that will be the issue, but the content of your speech that can get you into trouble. But there are a couple instances where yelling at a police officer could be enough to get a citation or arrest.
Generally yelling at a cop, or even yelling loud enough to draw a crowd, could be considered disturbing the peace in some jurisdictions. And you should be careful not to interfere with an officer's duties or investigation. This is easier to do by keeping a safe and respectable distance if you feel the need to make your voice heard.
While it's probably not a great idea to antagonize a police officer, in most cases you won't get arrested for insulting a cop. Courts have found that flipping off a cop isn't enough to warrant a traffic stop, and that verbally challenging officers during an investigation isn't enough to warrant an arrest. You may even get away with calling cops "pigs," although, again, we wouldn't recommend it unless you're comfortable with increased police scrutiny.
Where you can get in trouble, however, is "fighting words," defined as words that, "by their very utterance inflict injury or tend to incite an immediate breach of the peace" or have a "direct tendency to cause acts of violence by the person to whom, individually, the remark is addressed." Obviously this definition is a little fuzzy and varies by jurisdiction, but fighting words statutes may give officers a basis to arrest you.
If you've been arrested or charged with a crime for yelling or swearing at a police officer, you should refrain from hurling more insults at the cops and instead contact an experienced criminal defense attorney immediately to (calmly and quietly) discuss your case.