PWM on Piezoelectric sensor

i want to generate a 1 mhz pulse on one of my peizo sensor and want to receive the pulse on the other disk ,now right now i have generated a 1 mhz pulse of 1 disk now i want to receive it on the other
here is my code
void setup() {
// Set Pin 9 as output
Serial.begin(115200);
pinMode(9, OUTPUT);
pinMode(4, INPUT);

// Configure Timer1 for Fast PWM, mode 14 (WGM13:WGM10 = 1110)
TCCR1A = _BV(COM1A) |1 _BV(WGM11);
TCCR1B = _BV(WGM13) | _BV(2WGM1) | _BV(CS10); // No prescaler

// Set TOP value to 15 for 1 MHz
ICR1 = 15;

// Set duty cycle (50% duty)
OCR1A = 7;
}

void loop() {
int result = 0,final = 0;
for(int i = 0;i < 100;i++)
{
result += analogRead(4);
}
final = result/100;

Serial.println(final);
delay(1000);
// Timer1 is handling the PWM generation
}

You deal with waves, not pulses. Why do you use 1MHz for transmission of a static signal?

The analogRead() takes longer than 1µs, you'll never get meaningful readings at that frequency.

1 Like

please elaborate

What do you want to learn?

1 Like

Hi, @mubamistry2520
Welcome to the forum.

Is your project just experimentation or do you have a particular use for this method of communicating?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1 Like

i need to use it in my project

So you are sure that it can be made work?
Who told you so?

What is your project?

Tom.. :smiley: :+1: :coffee: :coffee: :australia:

i am trying to make an ultrasonic flow meter using arduino uno and two piezoelectric disks , first 1 peizo will send pulses and the other one will receive and then vice versa after this we will calculate the flow rate

In that case, you'd have to send a pulse and then time how long it takes to be recorded by the other transducer. Then do the same thing in the opposite direction and determine flow rate. The width of the pulse as such is not very relevant as long as it's dependably picked up by the other transducer.

(edited for correctness; apologies for earlier version)

Also, I've reported your thread for being a cross-post of this here: 1MHz PWM signal on Arduino Uno
And it looks like you've been working ont he same project for a while now and keep making new threads about it. Be warned that other participants on this forum tend to respond very negatively to this way of presenting yourself. Try to keep everything related to the same project in the same thread, try to offer complete information about the context, application and your requirements, and also be clear about what kind of hardware setup you're working with including diagrams and photos so we know what you have on hand.

I doubt that a normal cheap 20KHz piezo disk will move detectably with a 1MHz signal, and even without significant attenuation between the two, that a second piezo disk would translate that motion into a detectable 1MHz signal that a 1mV/count Arduino could detect.

What is frequency response of your disks?

1 Like

You can't, the Uno is not fast enough.

are you telling that uno cant receive signal of 1 mhz because i am able to generate 1 mhz signal

What is the distance the pulse will propagate before being detected?

If you are puling at 1MHz == 1us you will be transmitting a pulse.
Within that 1us you are trying to detect the pulse in the receiver

What is the medium that you are measuring flow?

Can you please tell us your electronics, programming, arduino, hardware experience?

Tom.. :smiley: :+1: :coffee: :australia:

i am a fresher with 6 months of experience, currently i am testing it in air

void setup() {
// Set Pin 9 as output
Serial.begin(115200);
pinMode(9, OUTPUT);
pinMode(4, INPUT);
//pinMode(3, OUTPUT);
//digitalWrite(3,HIGH);

// Configure Timer1 for Fast PWM, mode 14 (WGM13:WGM10 = 1110)
TCCR1A = _BV(COM1A1) | _BV(WGM11);
TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // No prescaler

// Set TOP value to 15 for 1 MHz
ICR1 = 15;

// Set duty cycle (50% duty)
OCR1A = 7;
}

void loop() {

int result = analogRead(4);
Serial.println(result);
//delay(100);
// Timer1 is handling the PWM generation
}

Yes

yes i came to know it now that it cant receive 1 mhz analog signal so which other mcu can i use

To show you why you can't capture a 1MHz signal using an Arduino ADC, I've done a test.

I modified your loop() function to turn pin12 high immediately before the Arduino does an analogue read, and then low again as soon as the analogue read was completed.

I used direct port manipulation so that it would have negligible effect on the speed of your code (each operation that I added only adds 62.5ns).

void loop() {
PORTB = B00010000;    // turn pin 12 high
int result = analogRead(4);
PORTB = B00000000;    // turn pin 12 low
Serial.println(result);
}

I monitored pin 12 on an oscilloscope together with the serial output on pin 1.
I'm feeding a 1MHz, 100mV pk-pk signal into analog pin A4.

Here are the results:

  • blue trace - pin12
  • green trace - serial output

Looking at the measurements towards the bottom of the screen, you can see that it takes 106µs to do the analogue to digital conversion.

It then takes a total of 425µs to do the serial printing, before it can do the next A to D conversion.

This means that you are only taking samples at a rate of 2.35kHz.

Now do you see why you can't detect the 1MHz signal?

Thanks bro you made it clear and easy for me to understand

That's not really the question, I think.

Go back to what I summarized in #10. You don't really need to do an ADC reading. What you need, is to send a signal from one transducer so the other can pick it up. You need to record the time when the second one picks up the signal from the first. This does not have to be an analog reading. You can amplify the signal the second transducer receives so that it can be read from a digital GPIO.

The signal's amplitude is irrelevant. What matters is the delay between sending the signal and receiving it. None of this requires analog readings.