Pepper or Cayenne Pepper does help stop dogs from peeing on carpet because dogs hate the smell of cayenne pepper and other pepper and will avoid the area and not pee on the carpet.
Many commercial products used to clean pet urine and or to prevent pets from peeing on carpet contain Cayenne Pepper.
When your dog pees on the carpet you can use some cleaner such as an enzymatic cleaner or vinegar, and allow the carpet to dry completely.
Then sprinkle some Cayenne pepper on the area where the dog has been peeing and the dog will start avoiding that area.
Also when you catch your dog about to pee on the carpet you should immediately redirect the dog outside and tell them that they are to pee outside and not inside the house.