40 Hz Square wave with 8kHz tone

I would suggest using delays. I know this is a bad word but I think, at least to get you going it would be the best way.

I'm not great with code but essentially you would:

  • Setup up a timer for 8kHz
  • Turn on the timer
  • delay(12)
  • delaymicroseconds(500)
  • turn off the timer
  • delay(12)
  • delaymicroseconds(500)
    Loop forever