# Why does this “square wave” have what seems to be a charging action on it's leading edge?

I'm trying to produce a basic square wave output across a resistor from an ATMega328P Xplained mini. VCC is 5 V.

Setup:

Code: (Stuff with DDC/PORTB is just to control the onboard LED)

#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
// Turn off global pullup disable
MCUCR &= ~(1<<PUD);

// Direct pins
PORTC = PORTC & ~(1<<PORTC1) | (1<<PORTC3);
DDRB |= (1<<DDB5);
DDRC |= (1<<DDC1) | (1<<DDC3);

while (1)
{

PORTB ^= (1<<PORTB5);
DDRC ^= (1<<DDC1);
_delay_us(500);

}
}


But when I place an oscilloscope probe on the PC1 side of the resistor, rather than a square wave I'm seeing this:

It seems strange to me that the output isn't just a square wave, I understand that resistors aren't ideal and that they have small capacitive and inductive elements to them but this seems excessive? Additionally, why is the peak voltage of the waveform only 600 mV?

• please provide a schematic diagram of the circuit instead of a picture of the board – jsotola Oct 29 '18 at 23:58
• I can't read the color code on that resistor (the colors are off, and what I read makes no sense), but it seems the the multiplier is black, i.e., zero. This means a very low value resistor in the tens of ohms. Is this true? – Edgar Brown Oct 30 '18 at 0:59
• If the nomenclature on the board is correct, and if I'm reading your code correctly, then you have PC3 connected to PC1, both PC3 and PC1 are writing '0' (0V), and you're alternating PC3 between an input and an output. I don't know what might be causing PC3 to float up, but that's most likely happening when it's in its high impedance state -- the sharp downward edge is happening when it's going low-impedance. If you want to generate a square wave, try leaving it set as an output, and alternate writing a 1 and a 0 to it. – TimWescott Oct 30 '18 at 1:22
• The resistor is green-blue-blue-gold, so 56 Mohm. PC3 is writing '1' and is held as an output (should be constant VCC = 5V) while PC1 writes '0' and alternates input and output. *That said, thinking about it Tim's setup sounds like it would make more sense for making a square wave. I'll give it a shot. – Eric Oct 30 '18 at 1:37

## 1 Answer

This line:

DDRC ^= (1<<DDC1);


is actually inverting the direction of the PC1 pin at every iteration of the while loop. This places the PC1 pin in output mode and probably LOW state during one half-cycle and then in Hi-Z mode during the next. The impedance of the PC1 pin is then 10k-100kΩ, so this acts as a very poor RC circuit with the parasitic capacitance of the breadboard and PCB.

Your square wave is an RC circuit for half a cycle then a driven LOW for the other half, hence the waveform you are seeing.

• So the breadboard actually has sufficient capacitance to give a non-negligible time constant in this situation? I always assumed that the breadboard rails would be fairly ideal conductors. – Eric Oct 30 '18 at 1:45
• Parasitic capacitance in breadboard is a well-known problem. In typical sub-gigahertz application you may never see it, but in this case you are mixing low parasitic capacitance with high impedance, which, to your timescale of 500us, is visible. – Charles Gervais Oct 30 '18 at 2:56