0

MCP2221 + 5v Buzzer
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

MCP2221 + 5v Buzzer

by scotch1 on Sun Feb 21, 2021 9:52 pm

Ref: https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221/gpio
how to turn on the 5v Buzzer https://www.adafruit.com/product/1536for predetermined amount of time?
Tried like an LED, no luck:
Tried only other example but confused on criket use: https://learn.adafruit.com/crickit-snake-bot/adding-a-warning-buzzer

Code: Select all | TOGGLE FULL SIZE
import time
import board
import digitalio


led = digitalio.DigitalInOut(board.G0)
led.direction = digitalio.Direction.OUTPUT

buzzer = digitalio.DigitalInOut(board.G1)
buzzer.direction = digitalio.Direction.OUTPUT
     
while True:
    led.value = True
    buzzer.value = True
    time.sleep(2)
    led.value = False
    buzzer.value = False
    time.sleep(2)


Wouldn't turn on.

tried to copy this for pi, change board call outs, but can't use rpi for MCP2221:
Code: Select all | TOGGLE FULL SIZE
import RPi.GPIO as GPIO
from time import sleep
#Disable warnings (optional)
GPIO.setwarnings(False)
#Select GPIO mode
GPIO.setmode(GPIO.BCM)
#Set buzzer - pin 23 as output
buzzer=G1
GPIO.setup(buzzer,GPIO.OUT)
#Run forever loop
while True:
    GPIO.output(buzzer,GPIO.HIGH)
    print ("Beep")
    sleep(0.5) # Delay in seconds
    GPIO.output(buzzer,GPIO.LOW)
    print ("No Beep")
    sleep(0.5)


Is there a special function to turn on a buzzer?

scotch1
 
Posts: 182
Joined: Mon Nov 26, 2012 12:13 am

Re: MCP2221 + 5v Buzzer

by adafruit_support_carter on Mon Feb 22, 2021 4:23 pm

Is the LED blinking at least?

Please post a photo of your setup showing how everything is connected.

adafruit_support_carter
 
Posts: 18729
Joined: Tue Nov 29, 2016 2:45 pm

Re: MCP2221 + 5v Buzzer

by scotch1 on Mon Feb 22, 2021 6:23 pm

Light goes on and off no prob. Nothing on the buzzer. It works on an arduino code ok. One pin to GND, the other to the Digital pin. LED is per the tutorial, works.
mcp2221_buzzerSM.JPG
mcp2221_buzzerSM.JPG (596.39 KiB) Viewed 98 times


Code: Select all | TOGGLE FULL SIZE
import time
import board
import digitalio


led = digitalio.DigitalInOut(board.G0)
led.direction = digitalio.Direction.OUTPUT

buzzer = digitalio.DigitalInOut(board.G1)
buzzer.direction = digitalio.Direction.OUTPUT
     
while True:
    led.value = True
    buzzer.value = True
    time.sleep(2)
    led.value = False
    buzzer.value = False
    time.sleep(2)
   

scotch1
 
Posts: 182
Joined: Mon Nov 26, 2012 12:13 am

Re: MCP2221 + 5v Buzzer

by adafruit_support_carter on Mon Feb 22, 2021 6:42 pm

That does not look like one of these:
https://www.adafruit.com/product/1536

It may require something different to drive it?

adafruit_support_carter
 
Posts: 18729
Joined: Tue Nov 29, 2016 2:45 pm

Re: MCP2221 + 5v Buzzer

by scotch1 on Tue Feb 23, 2021 2:19 am

Seems to work on Uno: https://www.velleman.eu/downloads/29/vma319_a4v01.pdf (tried it, was good). There is basic code in arduino for tone melody or something.

scotch1
 
Posts: 182
Joined: Mon Nov 26, 2012 12:13 am

Re: MCP2221 + 5v Buzzer

by adafruit_support_carter on Tue Feb 23, 2021 11:06 am

Maybe it's 5V only? Not sure. Also can't tell if maybe it's more of this style of piezo:
https://www.adafruit.com/product/160
which is driven differently than the PID 1536 buzzer.

There is basic code in arduino for tone melody or something.

Can you link to this?

adafruit_support_carter
 
Posts: 18729
Joined: Tue Nov 29, 2016 2:45 pm

Re: MCP2221 + 5v Buzzer

by scotch1 on Tue Feb 23, 2021 12:46 pm

Seems it's a 5V per description. Isn't the PI running on that also?

Here's the code that worked on arduino:

Code: Select all | TOGGLE FULL SIZE
/*
  Melody

  Plays a melody

  circuit:
  - 8 ohm speaker on digital pin 8

  created 21 Jan 2010
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Tone
*/

#include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
  // no need to repeat the melody.
}

scotch1
 
Posts: 182
Joined: Mon Nov 26, 2012 12:13 am

Re: MCP2221 + 5v Buzzer

by adafruit_support_carter on Tue Feb 23, 2021 1:06 pm

That code is using the Arduino tone library:
Code: Select all | TOGGLE FULL SIZE
        tone(8, melody[thisNote], noteDuration);

https://www.arduino.cc/reference/en/lan ... d-io/tone/

The tone library generates a square wave output. Your piezo is more like the PID 160 piezo and not like the PID 1536 piezo.

The MCP2221 can not generate PWM (square wave) output. So it will not work with your piezo.

adafruit_support_carter
 
Posts: 18729
Joined: Tue Nov 29, 2016 2:45 pm

Re: MCP2221 + 5v Buzzer

by scotch1 on Tue Feb 23, 2021 6:38 pm

gotcha: good to know. I was actually trying to text with code intended for a PI (other, code issue post), so it will can work on a pi right?

Would it work same as LED on and off on PI or need code to generate sound also?

scotch1
 
Posts: 182
Joined: Mon Nov 26, 2012 12:13 am

Re: MCP2221 + 5v Buzzer

by adafruit_support_carter on Tue Feb 23, 2021 6:46 pm

Turning an LED on/off only requires simple digital out. You can do that with either a Pi or the MCP2221.

The Pi also does not have great PWM output. Therefore it will not be great at driving that piezo.

adafruit_support_carter
 
Posts: 18729
Joined: Tue Nov 29, 2016 2:45 pm

Re: MCP2221 + 5v Buzzer

by scotch1 on Wed Feb 24, 2021 12:00 am

does this one https://www.adafruit.com/product/1536work with Pi and MCP2221 as a simple LED on/off?

scotch1
 
Posts: 182
Joined: Mon Nov 26, 2012 12:13 am

Re: MCP2221 + 5v Buzzer

by adafruit_support_carter on Wed Feb 24, 2021 11:28 am

It should. You can see on the product page how it refers to a "plain piezo", which is probably the style you currently have. The "AC signal" it refers to is the PWM output mentioned above.

adafruit_support_carter
 
Posts: 18729
Joined: Tue Nov 29, 2016 2:45 pm

Re: MCP2221 + 5v Buzzer

by scotch1 on Thu Feb 25, 2021 3:36 am

Sold!

scotch1
 
Posts: 182
Joined: Mon Nov 26, 2012 12:13 am

Please be positive and constructive with your questions and comments.