How to generate square wave(voltage pulses) or tone using photon?

I want to generate a square pulses using photon. Can any one tell me how to achieve that?

So… this…?
https://docs.particle.io/reference/firmware/photon/#tone-

2 Likes

I tried this code but its showing a error
expected constructor, destructor, or type conversion before ‘(’ token

I don’t have any idea regarding this please help me,

That sounds like you have a syntax error in your code. You should show us the code that gave that error.

1 Like

Just by looking at the error I’d expect your code to look like this

tone(D0, 1000, 10);
 
void setup() {
  // whatever ...
}

void loop() {
  // whatever ...
}

But if you copied the actual usage sample it should work

// EXAMPLE USAGE
// Plays a melody - Connect small speaker to analog pin A0

int speakerPin = A0;

// notes in the melody:
int melody[] = {1908,2551,2551,2273,2551,0,2024,1908}; //C4,G3,G3,A3,G3,0,B3,C4

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {4,8,8,4,4,4,4,4 };

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(speakerPin, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(speakerPin);
  }
}

Just plonking a function call in the global declaration section will never work :wink:

Functional calls need to be placed inside a function.