Using the expansion port

I purchased a Mini Buzzer at Radio Shack P/N 2730793. When attached to the 5VDC and ground on the expansion port it is loud so this should fix every ones needs But I do not know how to select the pinout. I am attempting to modify code from the Arduino learning area. I know that I will have to change things but their code assigns and “int speakerPin = 9;”. Sparki has 2 5VDC ports, but does it have a programmable pin? I have looked at the diagram of the expansion port and at the Arduinos pin outs I do not get it. Sorry I don’t know what I am doing here need help.

Code from Arduino instruction area

[code]int speakerPin = 9;

int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}

void playNote(char note, int duration) {
char names[] = { ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘a’, ‘b’, ‘C’ };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}

void setup() {
pinMode(speakerPin, OUTPUT);
}

void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == ’ ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}

// pause between notes
delay(tempo / 2); 

}
}

[/code]

Radio Shack P/N 2730793 is a “buzzer” which when +5V and ground are applied to its wire leads will produce a continuous buzz much like an alarm. It is not intended to be programmed like the speaker on the Sparki. It is loud but you will not get a variation of tones from it, but a monotonous sound. I do not think this will work for Sparki.

Ok, that said, does anyone know if an appropriate piezo electric buzzer would work on a pin out from the expansion slot. If so can anyone spread light on which pen out and how to select that pen, ie “int speakerPen = pen 9;”.

You should be able to do it by re-defining the buzzer port to whatever pin number you put it on (with the other pin on ground), like so:

#define BUZZER 11

static.arcbotics.com/wp-content/ … iagram.jpg

I maybe the thickest headed user of Sparki but I tried re-defining the expansion port pin 11 and then pin 8 as the buzzer nothing changes with the expansion port (and the buzzer on sparki’s back is still buzzing). How are the pin outs numbered. I have looked at the arduino pin outs and they have numbers and they may not be numbered from 1 to 24 or what ever. Are sparki’s numbered from the rear of the robot to the front, left to right or some other way. I am kind of stuck here. Also I do not believe #define BUZZER 11, did anything to the buzzer or the expansion slot. I attached the buzzer to a ground and then attached the red lead to each of the other pin outs one at a time. The tone does change when you get to lower voltages but it is not being controlled by the program below.

#include <Sparki.h>  // include the sparki library

void setup()
{
}

void loop()
{
    #define BUZZER 8; //should define pin 8 as the BUZZER controller
    sparki.beep(); // Sparki beeps!
    delay(1000); // wait a second (1000 milliseconds)
}

Did you put the define BUZZER in the loop function?

For that to work, you need to change it in the sparki.h file, then recompile your program.

Bet I’d give you a run for your money on that one :slight_smile:

Anyway, from what I can make out, there’s no pin 11 on the expansion port. Think Joe was only using it as an example. Here’s the layout of the port:

http://static.arcbotics.com/wp-content/uploads//2014/01/Expansion-Port-Diagram.jpg

Here’s what I can make out of the naming convention. For example: (1)PD2 & (2)PB3, equates to (1)Port D Pin 2 & (2)Port B Pin 3 respectively.

Use pages 3 & 4 on this ATmega32u4 data sheet (http://www.atmel.com/images/7766s.pdf) for pin identification. Pages 5 & 6 outline each pins’ usage.

Hope this helps a bit.

The define lines goes right after the Sparki.h line.

The pin number is the Arduino pin number listed here, no need to look on another page:

Here is the Sketch

Sparki Play the Scale
Copied from learn.adafruit.com/adafruit-ardu … ng-a-scale
Modified by Ralph Ward
3/12/2014

[code]
/*******************************************
Sparki Play the Scale
code copied from http://learn.adafruit.com/adafruit-arduino-lesson-10-making-sounds/playing-a-scale
Modified By Ralph Ward
3/12/2014

********************************************/
#include <Sparki.h> // include the sparki library


int speakerPin = 14;
 
int numTones = 10;
int tones[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440};
//            mid C  C#   D    D#   E    F    F#   G    G#   A
 
void setup()
{
  for (int i = 0; i < numTones; i++)
  {
    tone(speakerPin, tones[i]);
    delay(500);
  }
  noTone(speakerPin);
}
 
void loop()
{
}

Insert the positive wire of a pieso buzzer into the number 14 socket (forward, left) and the ground wire into a ground socket (3rd from rear,left).

This will play the scale, It is louder than the on board buzzer, I hope this works for you.

Thanks that works perfectly! The Piezo sitting in my parts bin even fit perfectly between those sockets :wink:

EDIT: Actually, I switched to using pin 8. Pin 14 seemed to stop working right after using other features of the robot.

Did you guys have to amplify it through a transistor? Or did you connect the piezo straight between pin 8 or 14 and GND?

I did not amplify it but that would be great. I really do not know how to do that Could you diagram that I would like to try that?

Here’s a simple circuit from TDK…

I am at odds with the Expansion Port. I have written a sketch that sends a loud tone to a piezo buzzer and blinks a LED. I have wired the buzzer to pin 14 and the 5V VCC (I also wired it to the GND but it is a lot louder on the VCC) the blinking LED is wired to pin 3 and Gnd. The buzzer and LED with a resistor are mounted on a small bread board which I put on top of the LCD. What I was trying to do is have Lights and Sirens and Sparki moving. I must be doing something wrong. The following sketch works for the Lights and Sirens.

[code] #include <Sparki.h> // include the sparki library
//This sketch uses the Expansion Port to send a loud tone to
//buzzer and bink a LED.

int speakerPin = 14;
int ledPin = 3;
int led = 3;

int numTones = 2;
int tones[] = {
2000, 1330};

void setup(){
pinMode(led, OUTPUT);

}

void loop(){

digitalWrite (led, HIGH); //turns led on
delay(200); //waits 200 ms
digitalWrite (led, LOW); //turns led off
for (int i = 0; i < numTones; i++)
{
tone(speakerPin, tones[i]);
delay(500);
}
noTone(speakerPin);

delay(200);
}[/code]

The problems occur when I try to add any other code to this sketch. Like in this code that I think should do the same thing as the first sketch but make Sparki move forward 10 cm. See the next to the last line of CODE. (caution if you upload this code to sparki it will freeze sparki and then you will have to reload the sparki software and reboot your sparki. I have been working on this for over a week. I have used other pins on the expansion port and tried to use other sparki code rather than moving forward. I get the same problem no matter what. I need your help, anyone see what I am doing wrong?

[code]#include <Sparki.h> // include the sparki library
//This sketch uses the Expansion Port to send a loud tone to
//buzzer and bink a LED.

int speakerPin = 14;
int ledPin = 3;
int led = 3;

int numTones = 2;
int tones[] = {
2000, 1330};

void setup(){
pinMode(led, OUTPUT);

}

void loop(){

digitalWrite (led, HIGH); //turns led on
delay(200); //waits 200 ms
digitalWrite (led, LOW); //turns led off
for (int i = 0; i < numTones; i++)
{
tone(speakerPin, tones[i]);
delay(500);
}
noTone(speakerPin);
sparki.moveForward(10);
delay(200);
}[/code]

Have you tried running it without your expansion board?

By reload and reboot, do you mean the hold-reset trick or something worse?

The problem is likely that the Sparki software, when its brought up, grabs control of the part of the processor that takes care of Tone (specifically its timer). You’d probably be better of changing the buzzer pin in the Sparki.h file instead of doing it manually. There’s a line where it defines BUZZER. Change that to the pin number you’re using for your own buzzer.

Latest attempt edited the Sparki.h file “buzzer 11”. To “buzzer 14” and rewrote the sketch to take out the references to speeker on pin 14. Nothing seemed to change, except when I removed the beadboard from the expansion port the weak on board buzzer tried to buzz the siren tones. This started out as a fun project but it has turned into just another dead end. It appears the expansion port also has many limitations. Are we getting close to any notable updates to the Sparki software? I keep hearing that it is about a week out. How are we doing with that?