Phaseshift analogRead wave

Hi All,

I'm new to arduino, the forum and programming basically. I'm in need of a simple phase shifting code for the wave that has been read. I'm using PWM to switch a mosfet which is supplied by 1.5V - in short, 1.5V square wave is fed to pin A1. At A2, I want a 180 degree phase shifted wave. I see a phaseshifted wave when I plot the values I see on the serial monitor (Please see attached screenshot) but when I connect a scope to the pin, I don't see anything (See WhatsApp image attached. Yellow - A1 signal, Blue - A2 signal.)

Here's my current code.

byte pwmpin = 3; // pins may be 3,5,6,9,10,11
byte pwmdutycycle = 128; // value may be 0 to 255

void setup(){
Serial.begin(9600);
pinMode (pwmpin, OUTPUT);
pinMode (A2, OUTPUT);

analogWrite (pwmpin, pwmdutycycle);
Serial.println();
}

void loop(){

int pwmVal = analogRead(A0);
float pwmwave = pwmVal * (5.0/1023.00);
Serial.println(pwmwave);

int input = analogRead(A1);
float onepointfive = input * (5.0/1023.00);
Serial.print("\t");
Serial.println(onepointfive);

int val = analogRead(A1); // read input value
float phaseshifted = val * (5.0/1023.00);
float low = 0.0;
float high = 1.5;
if (phaseshifted >= 1.2){
analogWrite(A2,0.00);
} else {
analogWrite(A2,1.5);
}

Serial.print("\t");
Serial.print("\t");
Serial.println(phaseshifted);

}

(deleted)

I am using a Nano.

I don't understand what you're trying to get out... You can't get a 1.5V inverted square wave out, but you could get a 5V inverted square wave (if that's what you want)...

[u]PWM[/u] is NOT analog. There is no digital-to-analog converter so you cannot get 1.5V out of the Arduino (without additional hardware).

You can write a value that averages 1.5V but I don't think that's what you want.

A square wave isn't really "analog" but you can use an analog input to read it.

If you want to invert the square wave and boost it to 5V you can simply write a high whenever the input is less than 0.75V and write a low whenever the input is greater than 0.75V. (3/4ths of a volt should read about 767 and there's no need to convert to voltage.)

Inverting is different from phase shifting. Imagine a square wave that reads 0, 5, 0, 5... An inverted signal will read 0, -5, 0, -5... I want a phase shifted one - it must read 5, 0, 5, 0...

And yes, as mentioned, I am using a mosfet to create a 1.5 V square wave. The mosfet is switched using the PWM pin. This won't answer my question but could you please provide the code to get a 1.5V square wave from the nano?

If you just invert the way you are trying to do, then you already have a phase shift.
Because of the slow-ish analogRead, and the slow printing you do after each read.
What frequency are you trying to phase shift.

Tell us more about that 1.5volt that you need.
A 1.5volt PWM signal can be made with a simple voltage divider on the Arduino output.
Leo..

I am trying to phaseshift a 490 Hz signal from the D3 pin on the nano.

I have a phaseshift from the logic...but I don't know why I can't just read it on the scope...it reads 0V on that pin. (Allow me to point back to the whatsapp image I have attached in the original post.)

I tried the voltage divider circuit. It was overpowering the sensor so I had to skip it. That is OK. I am more concerned about the phase shift or rather being able to read the phase shift.

haristadena:
I am trying to phaseshift a 490 Hz signal from the D3 pin on the nano.

I tried the voltage divider circuit. It was overpowering the sensor so I had to skip it.

Seems like we have an XY problem here.

I understand you want the Arduino to generate two complementary PWM outputs (opposite phase).

What sensor, and where does the 1.5volt fit in.
Leo..

I understand you want the Arduino to generate two complementary PWM outputs (opposite phase).

I'm not clear on what you actually want to do, but you can certainly set up the second hardware output on Timer2 (pin 11) with the opposite phase output. Here's an example of shifted square wave with fast pwm mode (980HZ). It is easily modified to the 490 Hz you are using, or to an arbitrary frequency. Duty cycle can also be modified from a square wave.

void setup() {
  pinMode(3, OUTPUT); //OC2B
  pinMode(11, OUTPUT); //OC2A
  TCCR2A = 0; //initialize register
  TCCR2B = 0; //in itialize register
  TCCR2A = _BV(WGM20) | _BV(WGM21); //set fast PWM
  TCCR2A |= _BV(COM2A1) | _BV(COM2A0); //Set OC2A on Compare Match, clear OC2A at BOTTOM,(inverting mode).
  TCCR2A |= _BV(COM2B1); //Clear OC2B on Compare Match, set OC2B at BOTTOM,(non-inverting mode).
  TCCR2B = _BV(CS22); //prescaler 64
  OCR2A = 127;
  OCR2B = 127;

}

void loop() {
}

I don't know why I can't just read it on the scope...it reads 0V on that pin.

It was explained previously that you can not analogWrite() on A2.

haristadena:
1.5V square wave is fed to pin A1. At A2, I want a 180 degree phase shifted wave.

Is this a school assignment or so? In the real world I'd use a 74HC04 inverter for a job like this.

You're doing analogRead() which takes about 100 us to complete - so your second wave is always lagging by that much. For your wave you'd have a 10% time lag just because of that, not counting the time it takes for your code to process the number and write it out to another pin (which should be far less).

You can improve big time of course by simply copying the value of one pin to another, but you're never going to get in to the nanoseconds it takes a 74HC04 to propagate your signal.

Yes! This is exactly what I wanted!

Sorry for a very basic question. How do you set the duty cycle in the code you provided?

Now, how do I reduce the amplitude from 5V to 1.5V without any external hardware? Thanks cattledog.

Sorry for a very basic question. How do you set the duty cycle in the code you provided?

The duty cycle is set by the OCR2A and OCR2B values. I'm unclear about the phasing requirements when the pulses are not 50% and each one is half the time period. You may need to use a dual sloped pwm mode. Using two different timers but synchronized might also be an option.

Now, how do I reduce the amplitude from 5V to 1.5V without any external hardware?

You will not be able to do that. Switching a 1.5 v source, like you are doing, or using an external voltage divider are your basic options.

What are you trying to achieve? What are you doing with the 1.5v square waves.

Once phase shifted, I am using op amps to invert the phase shifted signal and then adding it to the original pwm signal. The resultant wave is a square wave that plateaus at 1.5V and -1.5V. I am using this signal to power the sensor. If I don't supply a negative voltage, the sensor will get saturated.

To get an AC signal (positive and negative related to ground) from a 'DC square wave', you usually use a coupling capacitor. Not an opamp.

Still not clear what you're going to do with that signal.
Leo..

the sensor will get saturated.

This has now been asked several times

"WHAT SENSOR"