Learning experience

I recently purchased an oscilloscope and through this forum and an Arduino Uno I'm slowly learning how to use it.

Recently using the Arduino I made a simple sinewave generator that I was able to observe on my scope. The Arduino Uno was part of a Sunfounder basic Arduino kit. I ordered an Elegoo mega starter kit so I could expand my knowledge.

Well, instead of starting to build something new I uploaded the same sinewave sketch into the new Elegoo Uno board using the same code, board and parts. Low and behold the sinewave it produce on the scope was visible different and the voltages/frequencies were all over the place.

The lesson learned is that not all Arduino boards are the same. Now to find out why!!!

Well it’s time to learn how to read schematics and datasheets. You do not need to learn all the little details, but they are good for an overview.

There are many different microcontrollers and other components used on Arduino board. Some are so different that they have almost nothing in common.

The best is to get the name of the components and then get the datasheet from the original manufacturer. They all have a first "marketing" page that will give you an overview of microcontroller that you have on the board. A few pages in you usually can find a block diagram that gives you an overview of the different blocks inside the microcontroller.

You will be amazed how much cool stuff you can have available to you.

If the sine wave sketch used timer hardware registers, the behavior between the Arduino Uno and Arduino Mega could well be different. You will have required external hardware to make a sine wave generator with these Arduinos because there is no inbuilt DAC (digital to analog converter)

Post the sketch here perhaps? Most of us are fluent in reading code :slight_smile:

MarkT:
Post the sketch here perhaps? Most of us are fluent in reading code :slight_smile:
[/quote

The code is not mine but a fellow member here.

//code modified for improvement from http://forum.arduino.cc/index.php?topic=8563.0
//connect pin 9 -> 10k Ohm + (series with)100nF ceramic cap -> GND, tap the sinewave signal from the point at between the resistor and cap.

float wav1[3];//0 frequency, 1 unscaled amplitude, 2 is final amplitude
int average;
const int Pin = 9;
float time;
float percentage;
float templitude;
float offset = 2.5; // default value 2.5 volt as operating range voltage is 0~5V
float minOutputScale = 0.0;
float maxOutputScale = 5.0;
const int resolution = 1; //this determines the update speed. A lower number means a higher refresh rate.
const float pi = 3.14159;

void setup() {
wav1[0] = 50; //frequency of the sine wave
wav1[1] = 2.5; // 0V - 2.5V amplitude (Max amplitude + offset) value must not exceed the "maxOutputScale"
TCCR1B = TCCR1B & 0b11111000 | 1;//set timer 1B (pin 9) to 31250khz
pinMode(Pin, OUTPUT);
//Serial.begin(115200);//this is for debugging
}

void loop() {
time = micros()% 1000000;
percentage = time / 1000000;
templitude = sin(((percentage) * wav1[0]) * 2 * pi);
wav1[2] = (templitude * wav1[1]) + offset; //shift the origin of sinewave with offset.
average = mapf(wav1[2],minOutputScale,maxOutputScale,0,255);
analogWrite(9, average);//set output "voltage"
delayMicroseconds(resolution);//this is to give the micro time to set the "voltage"

}

// function to map float number with integer scale - courtesy of other developers.
long mapf(float x, float in_min, float in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

You’ve picked an interesting sketch for this learning experience. It does indeed directly manipulate a hardware register, so the behavior is specific to a processor type.
You might be lucky and changing the pin (timer pin OC1A) from pin 9 on the Uno to pin 11 on the Mega may work.
If not, you’ll have to start comparing the data sheets of the respective processors to check the behavior of timer1.

This line configures timer1:
TCCR1B = TCCR1B & 0b11111000 | 1 ; //set timer 1B (pin 9) to 31250khz

And this defines the pwm pin:
const int Pin = 9 ;

cyberwasp:
I ordered an Elegoo mega starter kit so I could expand my knowledge.

Well, instead of starting to build something new I uploaded the same sinewave sketch into the new Elegoo Uno board using the same code, board and parts.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

The Elegoo Mega starter kit comes with a Mega not a UNO, they are totally different animals.
Can you post a picture of both your controller boards please.
When you uploaded the sketch to the Elegoo what model controller did you set in the IDE, before uploading?

Thanks.. Tom.. :slight_smile:

The Elegoo Mega starter kit comes with a Mega not a UNO, they are totally different animals.
Can you post a picture of both your controller boards please.
When you uploaded the sketch to the Elegoo what model controller did you set in the IDE, before uploading?

Sorry Tom. I was using the reply button at the bottom of each message as it is with most of the message boards I belong to. Below is a photo of both boards!

I had it set to Arduino mega 2560

Tom, in the above message I used the image insertion tool with a link but it's not showing according to the forum tutorial http://forum.arduino.cc/index.php/topic,148850.0.html.

let's do it my way:

cyberwasp:
Tom, in the above message I used the image insertion tool with a link but it's not showing according to the forum tutorial http://forum.arduino.cc/index.php/topic,148850.0.html.

let's do it my way:

Microsoft OneDrive - Access files anywhere. Create docs with free Office Online.

Or you could do like everyone else does and use the attachments feature this board has available under the post form. (Not the quick reply below). Add it as an attachment. Then post. Afterwards there will be a link below your post to the image. You can then right click and copy the address. Then click on the "quick edit" link below your post and type [img]paste your link here[/img]. Going the extra mile yourself is greatly appreciated at this board.