PWM: Read square wave signal

Hello everyone,
I want to use pwm pin to generate a square wave signal with duty cycle of 50%.
Simply, I wrote:
analogWrite(pwm pin,128);

I want to read the output value to find when the signal change from 0 to 1.
I have tried to use the digitalRead function, but it doesn't work because I always get the value 0.
I want to get something about this:
0,0,............0,1,1,...............,1,0,0,etc.

The only case that digitalRead works is when the duty cycle is 100% or 0%, namely a constant signal.
Thanks in advance for any help.

Where do you aim to see 0,0,............0,1,1,...............,1,0,0,etc. ?

Simply, I wrote:
analogWrite(pwm pin,128);

Why not post what you actually wrote?
Aka cut-and-paste

I didn’t write any code because I don’t know which function use.
Look the image that I attached.
With analogWrite(pwm pin, 128) I generate a square waveform.
I want to read the value of the signal to check if in a certain time the voltage is equal to 5V (HIGH) or 0V (LOW).
Is there some function to do this?

square wave.PNG

With analogWrite(pwm pin, 128) I generate a square waveform.

That code cannot possibly compile so WHY DON'T YOU POST THE ACTUAL CODE YOU WROTE?

void setup() {
 pinMode(3, OUTPUT);
 pinMode(13, OUTPUT);
 analogWrite(3,128); // duty cycle 50%
}

void loop() {
 if(digitalRead(3)==HIGH)
 {
   digitalWrite(13,HIGH);
 }
 else
 {
   digitalWrite(13,LOW);
 }
 
 
}

Go back and edit that. You need to select the code before you tag it.

Edit - thanks... now, what does your code do or not do that is a problem for you?

It's just an example to check if I can read the correct value of pwm pin.

albertoM90:
It's just an example to check if I can read the correct value of pwm pin.

Right, we know that. What is the result?

The led that I connect to pin 13 is off because the value of digitalRead is always 0. The led doesn't blink.

int digitalRead(uint8_t pin)
{
	uint8_t timer = digitalPinToTimer(pin);
	uint8_t bit = digitalPinToBitMask(pin);
	uint8_t port = digitalPinToPort(pin);

	if (port == NOT_A_PIN) return LOW;

	// If the pin that support PWM output, we need to turn it off
	// before getting a digital reading.
	if (timer != NOT_ON_TIMER) turnOffPWM(timer);

	if (*portInputRegister(port) & bit) return HIGH;
	return LOW;
}

albertoM90:
The led that I connect to pin 13

Did you use a current limiting resistor?

No, I don't use any resistors.
The code works, but can you explain me how to read the digital value of a pwm pin?
I want to check the value to do other things.
Thanks.

The code works, but can you explain me how to read the digital value of a pwm pin?

By jumpering the output pin to an input pin?

 if(digitalRead(3)==HIGH)
 {
   digitalWrite(13,HIGH);
 }
 else
 {
   digitalWrite(13,LOW);
 }

Are you getting paid by the line?

digitalWrite(13,digitalRead(3));

albertoM90:
No, I don't use any resistors.

That is a bad idea. You can burn out an Arduino output because it can exceed the maximum output current rating for a pin.

void setup() {
 pinMode(3, OUTPUT);
 pinMode(13, OUTPUT);
 analogWrite(3,128); // duty cycle 50%
 pinMode(3,INPUT);
}

void loop() {
 if(digitalRead(3)==HIGH)
 {
   digitalWrite(13,HIGH);
 }
 else
 {
   digitalWrite(13,LOW);
 }
 
 
}

I have tried to jump from output to input adding the line pinMode(3,INPUT), but the situation doesn't change.

analogWrite(3,128); // duty cycle 50%
 pinMode(3,INPUT);

It may be instructive to look at the source of pinMode.

You are right, but to be honest I'm at the beginning with Arduino and I thought that my question was a simple task to solve.

The best way to learn is to do.

AWOL:
It may be instructive to look at the source of pinMode.

Which is in reply #10. Especially the comments.