I've been trying to make a squarewave using the feather m4 express with the zerotimer library but i'm still a novice in programming. i think that i got it to start the hardware timer but i have no idea how to tie a pulse signal to it.
(i can show the different variations of code i've used uptill this point).
oh btw i took for most of my tries code from other people online and messed around with the zerotimer librarie functions to try and get it to do exactly what i wanted it to do.
Welcome to the forum
Please post your best effort at programming this by posting your sketch or sketches here, using code tags when you do
Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
It is also helpful to post full error messages in code tags as it makes it easier to scroll through them and copy them for examination
I have this one:
#include <SAMDTimerInterrupt.h>
#include <Arduino.h>
#include "Adafruit_ZeroTimer.h"
// timer tester
Adafruit_ZeroTimer zt3 = Adafruit_ZeroTimer(3);
#define TIMER3_OUT0 10
void setup() {
Serial.begin(115200);
//while(!Serial);
Serial.println("Timer PWM tester");
delay(1000);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
delay(1000);
digitalWrite(13, HIGH);
// --- CONFIGURATION ---
#define TIMER_NUMBER 3 // TC3 timer
#define OUTPUT_PIN 10 // PWM-capable pin (D12)
#define FREQUENCY_HZ 1000 // Desired square wave frequency
#define DUTY_CYCLE 50 // 50% for square wave
// ---------------------
int enablePWM(uint8_t channel, uint8_t duty);
/********************* Timer #3, 16 bit, two PWM outs, period = 65535 */
zt3.configure(TC_CLOCK_PRESCALER_DIV1, // prescaler
TC_COUNTER_SIZE_16BIT, // bit width of timer/counter
TC_WAVE_GENERATION_NORMAL_PWM // frequency or PWM mode
);
if (!zt3.PWMout(true, 0, 10)) {
Serial.println("Failed to configure PWM output");
} else {
Serial.println("configure PWM output 0 ok");
}
delay(5000);
if (!zt3.PWMout(true, 0, 10)) {
Serial.println("Failed to configure PWM output again");
} else {
Serial.println("configure PWM output 0 ok again");
}
zt3.PWMout(10, 10, 10);
if (!zt3.PWMout(10, 10, 10)) {
Serial.println("timer on");
} else {
Serial.println("timer off");
}
delay(20000);
zt3.PWMout(0, 0, 10);
if (!zt3.PWMout(10, 10, 10)) {
Serial.println("timer on");
} else {
Serial.println("timer off");
}
void setPeriodMatch(uint32_t period, uint32_t match, uint8_t channum = TIMER3_OUT0);
zt3.setCallback(true, TC_CALLBACK_CC_CHANNEL0, TC3_Handler);
zt3.enable(true);
}
uint16_t c = 0;
void loop() {
zt3.setCompare(0, c);
zt3.setCompare(1, 0xFFFF - c);
}
And I have this one:
#include <SAMDTimerInterrupt.h>
#include <Arduino.h>
#include "Adafruit_ZeroTimer.h"
// timer tester
Adafruit_ZeroTimer zt3 = Adafruit_ZeroTimer(3);
#define TIMER3_OUT0 10
#define TIMER3_OUT1 11
void setup() {
Serial.begin(115200);
//while(!Serial);
Serial.println("Timer PWM tester");
delay(1000);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
delay(1000);
digitalWrite(13, HIGH);
// --- CONFIGURATION ---
#define TIMER_NUMBER 3 // TC3 timer
#define OUTPUT_PIN 10 // PWM-capable pin (D12)
#define FREQUENCY_HZ 1000 // Desired square wave frequency
#define DUTY_CYCLE 50 // 50% for square wave
// ---------------------
int enablePWM(uint8_t channel, uint8_t duty);
/********************* Timer #3, 16 bit, two PWM outs, period = 65535 */
zt3.configure(TC_CLOCK_PRESCALER_DIV1, // prescaler
TC_COUNTER_SIZE_16BIT, // bit width of timer/counter
TC_WAVE_GENERATION_NORMAL_PWM // frequency or PWM mode
);
if (! zt3.PWMout(true, 0, TIMER3_OUT0)) {
Serial.println("Failed to configure PWM output");
}
else
{
Serial.println("configure PWM output 0 ok");
}
delay(5000);
if (! zt3.PWMout(true, 0, TIMER3_OUT0)) {
Serial.println("Failed to configure PWM output again");
}
else
{
Serial.println("configure PWM output 0 ok again");
}
zt3.periode(1000)
zt3.
if (! zt3.PWMout(true, 1, TIMER3_OUT1)) {
Serial.println("Failed to configure PWM output");
}
else
{
Serial.println("configure PWM output 1 ok");
}
delay(5000);
if (! zt3.PWMout(true, 1, TIMER3_OUT1)) {
Serial.println("Failed to configure PWM output again");
}
else
{
Serial.println("configure PWM output 1 ok again");
}
void setPeriodMatch(uint32_t period, uint32_t match, uint8_t channum = TIMER3_OUT0);
zt3.setCallback(true, TC_CALLBACK_CC_CHANNEL0, TC3_Handler);
zt3.setCallback(true, TC_CALLBACK_CC_CHANNEL1, TC3_Handler);
zt3.enable(true);
}
uint16_t c = 0;
void loop() {
zt3.setCompare(0, c);
zt3.setCompare(1, 0xFFFF-c);
these two are the ones that have worked the best uptil this point.
What's the purpose of your project?
Is it a square wave generator for something else?
It always helps to put your project in some sort of context
I don't know anything about the library you refer to, but even the most basic Arduino could provide the basis of a generator.
The important things will what accuracy are you looking for, frequency etc.
I'm sure that a bit more digging around will throw up solutions.
Bear in mind, the timing on Arduino boards isn't Caesium clock accurate.
@mental-pendulum
You was told:
According to view of your published code, you ignored this advice...
Also your inserted code is not complete, it ends with this:
i was tasked to make a pulse width genarotor for the purpose of testing pulse led's.
they want it to be pretty accurate as to be capable of simulating different specifications that the light's might be needed for.
sorry for that, i didn;t have the code in the ide i had it in a text file so i just copy and pasted it right over.
and for the second thing, i don't know man thats how it ended in the previous version of that code (the one that someone else wrote), i used it because again i'm not proficient in coding and this is my first actual assignment where i have to use it extensively.
OK, still pretty vague.
How exactly are you going test these LED?
The Arduino might generate a square wave, but how are you going to see if that's correct? Ideally, you need to be able to see way the LEDs are being switched on and off on something like an oscilloscope.
An oscilloscope tells you all you need. The amplitude (maximum voltage), the accuracy of the square wave, the frequency etc.
You might do well to see how one of the most basic examples in the IDE works, i.e. Blink.
You can't visually tell the difference between 1000 Hz and 2000 Hz or if it really is a good waveform.
What I'm getting at is the need to have a clear idea of the objective is, and all the steps involved in getting there.
A school or college project? Makes no difference to me, but it might have a bearing on how it's assessed.
You can't use this code in that form as you inserted it here. This code will never compile.
but it did though
It means that the code you run and the code you posted are different codes.
This end of code is a syntax error:
Most probably you lost some lines while copied the code to the forum.
Of course! It's awesome that you're diving into hardware timers with the Feather M4 — that's a great way to generate precise signals. Let me break down how to think about this.
The key concept is that you don't "tie" the pulse directly to the timer. Instead, you use the timer's interrupts to change a digital output. Here's a simple framework:
cpp
#include <ZeroTimer.h> const int outputPin = 13; // Built-in LED pin void setup() { pinMode(outputPin, OUTPUT); // Initialize timer for 1kHz frequency (0.5ms high, 0.5ms low) ZeroTimer.init(1000); // Frequency in Hz // Attach our interrupt function ZeroTimer.attachInterrupt(toggleOutput); ZeroTimer.start(); } void toggleOutput() { digitalWrite(outputPin, !digitalRead(outputPin)); } void loop() { // Your other code here - the square wave runs independently! }
How this works:
- The timer runs independently in the background
- At your specified frequency, it calls
toggleOutput() - That function simply flips the pin state (HIGH→LOW or LOW→HIGH)
- Each period contains both states = square wave
To adjust frequency:
- Change the value in
ZeroTimer.init(YOUR_FREQUENCY_HZ)
For duty cycle control (if you need more than 50/50), you'd need to:
- Use two different compare values
- Switch between them in the interrupt
If you'd like to share one of your code attempts, I can help spot where the connection might be missing! Hardware timers have a learning curve but are incredibly powerful once you get them working.
the eventual goal of the project is to see weather the lights can follow the square wave up to a certain accuracy. but because I have a limited time, i was tasked to just focus on replicating the actions of a function generator. i need to make the lights pulse. i need to be capable of telling it when and how to pulse. we already have an oscilloscope so i don't need to build that into the project but i do need to be capable of telling the lights to follow a pulse, for that to happen i need to generate a consistent pulse. and i am trying to do that using the hardware timers on the feather m4 because when u use software the pulse isn't consistent.
I wonder what this means? How can the lights not follow the input signal waveform?
Please explain what the above means.
It seems to me that all what you need in your project is a simple blink code.
@mental-pendulum
About the code you posted. I clean and format it.
The result is below:
#include <SAMDTimerInterrupt.h>
#include <Arduino.h>
#include "Adafruit_ZeroTimer.h"
// timer tester
Adafruit_ZeroTimer zt3 = Adafruit_ZeroTimer(3);
#define TIMER3_OUT0 10
void setup() {
Serial.begin(115200);
//while(!Serial);
Serial.println("Timer PWM tester");
delay(1000);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
delay(1000);
digitalWrite(13, HIGH);
// --- CONFIGURATION ---
#define TIMER_NUMBER 3 // TC3 timer
#define OUTPUT_PIN 10 // PWM-capable pin (D12)
#define FREQUENCY_HZ 1000 // Desired square wave frequency
#define DUTY_CYCLE 50 // 50% for square wave
// ---------------------
int enablePWM(uint8_t channel, uint8_t duty);
/********************* Timer #3, 16 bit, two PWM outs, period = 65535 */
zt3.configure(TC_CLOCK_PRESCALER_DIV1, // prescaler
TC_COUNTER_SIZE_16BIT, // bit width of timer/counter
TC_WAVE_GENERATION_NORMAL_PWM // frequency or PWM mode
);
if (!zt3.PWMout(true, 0, 10)) {
Serial.println("Failed to configure PWM output");
} else {
Serial.println("configure PWM output 0 ok");
}
delay(5000);
if (!zt3.PWMout(true, 0, 10)) {
Serial.println("Failed to configure PWM output again");
} else {
Serial.println("configure PWM output 0 ok again");
}
zt3.PWMout(10, 10, 10);
if (!zt3.PWMout(10, 10, 10)) {
Serial.println("timer on");
} else {
Serial.println("timer off");
}
delay(20000);
zt3.PWMout(0, 0, 10);
if (!zt3.PWMout(10, 10, 10)) {
Serial.println("timer on");
} else {
Serial.println("timer off");
}
void setPeriodMatch(uint32_t period, uint32_t match, uint8_t channum = TIMER3_OUT0);
zt3.setCallback(true, TC_CALLBACK_CC_CHANNEL0, TC3_Handler);
zt3.enable(true);
}
uint16_t c = 0;
void loop() {
zt3.setCompare(0, c);
zt3.setCompare(1, 0xFFFF - c);
}
The code is a jumbled collection of pieces from different sketches. Function declarations placed in the setup can't be found there. This code has syntax errors and not compile. Therefore, this code couldn't be what you used.
Please show your REAL code.
if you tell it to generate a wave it ups the voltage, and with every piece of electronics you will find that there will be a slight delay between the signal and when the hardware follows that signal, depending on the lights your dealing with. if the lights take too long to turn on then that's a faulty light.
and i tried to use digitalwrite to generate a pulse and it worked but it wasn't consistent, the frequency would vary. me and my employer think it's because the microcontroller doesn't have the processing power to keep having to reread the values in the code and then continuously having to generate a pulse. it can't keep up.
i'm not kidding for me it compiles just fine, and yes it's a jumbled mess because it's me trying to find what works.
This is getting complicated.
Of course the LEDs will respond to the square wave.
Are you trying to see if there's a difference between the Arduino output and how the LED reacts?
It important to have a clear idea of what you want, especially if time is limited.
If I were you, I'd get the scope running.
But be in no doubt that for all practical purposes, the LEDs will follow the digital output of the Arduino.
Don't you see the errors I've mentioned? Don't you understand a single line of your own code?