Im attempting to control the pitch of a pulse that i generated using a pot. How do i do that

Here is my code.

#define sig_output 3 // Signal Output Pin D3
int pulse_width = 0;
int pulse_width_step = 2;
int sensorValue = 0;
int sensorPin = A4;

void setup() {
  pinMode(sig_output, OUTPUT);
  pinMode(sensorPin, INPUT);
  Serial.begin(9600);
}
void loop() {
  sensorValue = analogRead(sensorPin);
  analogWrite(sig_output, pulse_width);
  pulse_width = pulse_width + pulse_width_step;
  if (pulse_width == 256) {
    pulse_width = 0;
  }
  delay(4);
  Serial.println(sensorValue);
}

What do you by the "pitch"?

Please, post code, not pictures of code. So lazy.

Hello I fixed it. i apologise. First time posting here. by pitch I mean changing to different notes on a scale for example.

A pulse doesn't have a tone, just a duration.

Have you tried the tone () function?

Please remember to use code tags when posting code.

Which Arduino are you using where Pin D5 has the numerical value of 3?

D5Pin3

Sorry ya my bad i was trying out something its pin 3

So, correct the comment of the codes of post #1 and repost it using code tag (</>).

analogWrite produces a single frequency (pitch), but with varying duty-cycle
(ratio of on to off)

Facing difficulties to align the above definition of duty cycle with what I know:
duty cycle = ratio of ON to period.

Quote from net:
A compressor, for example, that operates for 1 minute and then shuts off for 99 minutes, is said to have a duty cycle of 1 / 100 , or 1 percent .

hi so i took your advice to use the tone function as it allows me to now control the pitch/notes of the tone. I also added another variable that is controlled by a pot to control the delay of that tone as seen below. Now im wondering if i can set certain parameters from 0 to 65000 using a loop that only allows the user to pick a specific note in a scale for example a pentatonic scale.

int outputPin = 3;
int PitchPin = A4;
int DelayPin = A3;
int Pitch = 0;
int Delay = 0;
void setup() {
  // put your setup code here, to run once:
  pinMode(outputPin, OUTPUT);
  pinMode(PitchPin, INPUT);
  pinMode(DelayPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  Pitch = analogRead(PitchPin);
  Delay = analogRead(DelayPin);
  tone(outputPin,Pitch, Delay);
  delay(Delay);

}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.