Ultrasound with Arduino UNO

I am working on a insect attractor. For this project I need a way to produce ultrasound. First I was planning to use piezo buzzer, but I couldn't find a way to buy it without paying ten times of its cost for delivery. I found a local electronics shop where I can buy a dynamic (the one you find in cars, speakers, etc.), but I don't know how to interact with them. Are they similar to piezo buzzers - so you can just plug it into your chain, or you need extra parts for it? Are they even suitable for producing ultra sound?

What exactly is a "dynamic"?

Is there any particular "produce ultrasound" goal, or do you imagine any acoustic vibration above 20 kHz will attract the insects?

AWOL:
What exactly is a "dynamic"?

Sound emitter found in speakers, cars, etc.

There's a range o ffrequencies certain insects use when looking for a mating partner.

Sound emitter found in speakers, cars,

Voice coil?

Barsik_The_CaT:
There's a range o ffrequencies certain insects use when looking for a mating partner.

Are you going to be more specific about these frequencies?

Or do you imagine people who might help you will first research these sounds, so they can then assist you with electronics and programming advice?

Can you understand how the lack of specific details in your question is limiting your opportunity to receive useful answers?

While all loudspeakers are technically electro-dynamic devices (they all convert electric signals to dynamic motion), I believe it's common jargon in the car stereo market to call tweeters using a permanent magnet and electromagnetic coil "dynamic" to distinguish them from the other common type which use piezoelectric effect to move the sound emitting surface. Other types like electrostatic speakers aren't sold in the auto market.

Physical construction greatly affects the frequency response of any speaker. But generally speaking, piezoelectric speakers are better than magnetic coil types for ultrasonic frequencies.

AWOL:
Voice coil?

I guess I have wrong idea about what 'speaker' is.

I'll get into specific frequencies once university will start working again and I'll be able to meet with certain people. Right now I only need to know if it's possible to produce ultrasound with a speaker, how to connect it, etc. For now I'm going to test things with piezo buzzer (5v, 75 dB) and arduino uno.

Maybe use the TimerOne library. It's the easy way to get PWM output with control of the frequency.

You can also access the timer's registers directly, but if you take that approach your code will be tightly tied to using on Arduino Uno.

Barsik_The_CaT:
with piezo buzzer (5v, 75 dB)

Here's another case where you've missed an opportunity for help. Instead of typing a part number or posting a link to the datasheet or tech info for the exact device you're using, you typed only "piezo buzzer (5v, 75 dB)".

We can only guess what that might be. If you'd given more specific info, we could know for sure what it is, and give you some confirmation or clear advice whether it can achieve your ultrasonic goal.

Many products sold as "buzzer" have a built-in oscillator. Those can't be used for ultrasonic frequencies, or really any frequency other than the ones its oscillator produces. Worse yet, the method of using those buzzers is completely different from other types which could create ultrasonic tones, so if you've got that type (a big unknown), your effort invested using it will be a fruitless path.

But that's all guesswork, because I don't know anything more than "piezo buzzer (5v, 75 dB)". If you had posted better specific technical info, I could have spent the time writing this message giving you a much better, more specific opinion. Can you understand how your lack of specifics wastes other people's time and squanders your opportunity to get useful help?

Do you know how hard it is to get into something you know nothing about, don't know where to get info about and you can't even properly ask about?
It's from russian manufacturer, so I can't give you link (unless you know russian, ofc), but it's operating voltage is 5v (perfect for arduino uno, from what I understood), but it's only capable of emitting frequencies from 1.4 to 4 khz. Guess it's gonna be much harder to buy an ultrasound one.

Digital logic schematics - converted to arduino code:

#include <avr/wdt.h> // watchdog

#define LED 13
byte SW1 = 1;   // 0 = insect, 1 = animals

    // binary counter 4020
    // bad numbering Q0 = Q1 etc
    byte Q01 = 0;
    byte Q04 = 0;
    byte Q06 = 0;
    byte Q08 = 0;
    byte Q10 = 0;
    byte Q12 = 0;
    byte Q13 = 0;
    byte Q14 = 0;

    byte A11 = 0;
    byte A12 = 0;
    byte A09 = 0;
    byte A10 = 0;



void hash_4020_1(){

        
        for (unsigned int i = 0; i <= 16383 ; i++){     // 4020 binary counter cascade

            Q01 = i & 1 ; //  CMOS CD4020
            Q04 = i>>3 & 1 ;
            Q06 = i>>5 & 1 ;
            Q08 = i>>7 & 1 ;
            Q10 = i>>9 & 1 ;
            Q12 = i>>11 & 1 ;
            Q13 = i>>12 & 1 ;
            Q14 = i>>13 & 1 ;

            if (SW1 == 0)   // switch animals - insect
                A12 = Q12 | Q08 ;         // diode logic
            else 
                A12 = Q12 | Q08 | Q04 ;   // diode logic
            
            A11 = not(A12 | Q01) ;  // logic NOR CMOS CD4011
            A10 = not(A11 | A12) ;  

            bitWrite(PORTB,0,A11);// D8     // output positive
            bitWrite(PORTB,2,A10);// D10    // output negatoive

             bitWrite(PORTB,5,Q14); // D13   // BLIK

	     if     (Q10 == 0 and Q13 == 0) delayMicroseconds(15);// oscillator frequency selector (left portion 4020)
            else if(Q10 == 1 and Q13 == 0) delayMicroseconds(6);
            else if(Q10 == 0 and Q13 == 1) delayMicroseconds(6); // 20, 32, 40kHz = 50, 31, 25 us, 10us is delay in processing of logic code
            else if(Q10 == 1 and Q13 == 1) delayMicroseconds(2);
                  
         }
    }

void setup() {
     wdt_enable(WDTO_8S); 

     pinMode(LED, OUTPUT);
     pinMode(4, INPUT_PULLUP);
     digitalWrite(LED, 1);
     delay(1000);
     digitalWrite(LED, 0);

     pinMode(8, OUTPUT);
     pinMode(10, OUTPUT);
   
     SW1=digitalRead(4);

     wdt_reset();
}

void loop() {
    hash_4020_1(); 
    wdt_reset();
}

ultrasonic speaker:
https://www.aliexpress.com/item/38MM-Ultrasonic-speaker-water-proof-high-quality-dimention-38mm-free-shipping/1707109380.html

note that original piezzo is 100nF, this is 50nF. You need make some corrections with LC filter, use resistor about 20 Ohms in series if no filter used.

Digital logic schematics - converted to arduino code:

Don't be stupid.

Please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

no formatting buttons here

No excuse use raw html code tags. Or get yourself a proper browser.

Plus your statement that the code somehow replaces all that circuitry is wrong. We aim to provide good quality help here, not rubbish.

All logic circuity of course. You still need power driver . I was try to use TDA2822M (suitable up to 100kHz; use 100nF caps only instead of reccomended!) which was pretty hot at Vcc=12V but still OK for many weeks in continous load.

New version made from more advanced solution have 8 selectable modes for different kinds of animals/pests.