Storing Cookies (See : http://ec.europa.eu/ipg/basics/legal/cookies/index_en.htm ) help us to bring you our services at overunity.com . If you use this website and our services you declare yourself okay with using cookies .More Infos here:
https://overunity.com/5553/privacy-policy/
If you do not agree with storing cookies, please LEAVE this website now. From the 25th of May 2018, every existing user has to accept the GDPR agreement at first login. If a user is unwilling to accept the GDPR, he should email us and request to erase his account. Many thanks for your understanding

User Menu

Custom Search

Author Topic: Arduino signal generator  (Read 32090 times)

ayeaye

  • Hero Member
  • *****
  • Posts: 866
Re: Arduino signal generator
« Reply #15 on: November 01, 2016, 09:28:37 PM »
Ok, now it rounds, and is a bit shorter, too.

Quote
#include <avr/sleep.h>

#define BUFSIZE 60

float getvalue(const char *text)
{
   char buffer[BUFSIZE], ch;

   Serial.println(text);
   for (sprintf(buffer, ""); strlen(buffer) < BUFSIZE - 1;)
      if (Serial.available()) {
         if ((ch = Serial.read()) == '\n') break;
         sprintf(buffer, "%s%c", buffer, ch);
      }
   return atof(buffer);
}

void setup()
{
   Serial.begin(9600);
   pinMode(10, OUTPUT);
}

void loop()
{
   float frequency, duty;
   unsigned long clock, period, pulsewidth;

   frequency = getvalue("Frequency?");
   duty = getvalue("Duty Cycle?");
   clock = frequency > 300. ? 16000000l : 250000l;
   period = (unsigned long) (clock / frequency + 0.5);
   if (period < 2) period = 2;
   if (period > 65000l) period = 65000l;
   pulsewidth = (unsigned long) (period * duty / 100 + 0.5);
   if (pulsewidth < 1) pulsewidth = 1;
   if (pulsewidth > period - 1) pulsewidth = period - 1;
   /* Timer1 fast PWM mode */
   TCCR1A = _BV(WGM11) | _BV(WGM10) | _BV(COM1B1);
   if (frequency > 300.)   /* No prescaling */
      TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
   else         /* 64 prescaling */
      TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10) | _BV(CS11);
   OCR1A = period - 1;
   OCR1B = pulsewidth - 1;
   Serial.print("Frequency ");
   Serial.print((float) clock / period);
   Serial.print(", Duty Cycle ");
   Serial.println(pulsewidth * 100. / period);
   Serial.println();
   delay(100);
   while (!Serial.available()) {
      set_sleep_mode(SLEEP_MODE_IDLE);
      sleep_enable();
      sleep_mode();
      sleep_disable();
   }
   TCCR1A = _BV(WGM11) | _BV(WGM10) | _BV(COM1B1);
   TCCR1B = _BV(WGM13) | _BV(WGM12);   /* Timer stop */
   TCNT1 = 0;            /* Timer value 0 */
   digitalWrite(10, 0);         /* Output 0 */
   Serial.read();
}

ayeaye

  • Hero Member
  • *****
  • Posts: 866
Re: Arduino signal generator
« Reply #16 on: November 03, 2016, 12:13:13 AM »
Ok, let's make a small change, to get rid of that nasty warning. It evidently worked, and corresponded to the C/C++ standards too, but when Arduino and gnu perhaps, has such requirements about sprintf, then let it have that. The rest of the warnings there were about the Arduino system code, about its serial interface, like an unused variable there, which in no way could been caused by my code. When compiling the second time there are no warnings, it doesn't show them more, because these are not relevant to the compiled code.

Quote
#include <avr/sleep.h>

#define BUFSIZE 60

float getvalue(const char *text)
{
   char buffer[BUFSIZE], ch;

   Serial.println(text);
   for (sprintf(buffer, "%c", '\0'); strlen(buffer) < BUFSIZE - 1;)
      if (Serial.available()) {
         if ((ch = Serial.read()) == '\n') break;
         sprintf(buffer, "%s%c", buffer, ch);
      }
   return atof(buffer);
}

void setup()
{
   Serial.begin(9600);
   pinMode(10, OUTPUT);
}

void loop()
{
   float frequency, duty;
   unsigned long clock, period, pulsewidth;

   frequency = getvalue("Frequency?");
   duty = getvalue("Duty Cycle?");
   clock = frequency > 300. ? 16000000l : 250000l;
   period = (unsigned long) (clock / frequency + 0.5);
   if (period < 2) period = 2;
   if (period > 65000l) period = 65000l;
   pulsewidth = (unsigned long) (period * duty / 100 + 0.5);
   if (pulsewidth < 1) pulsewidth = 1;
   if (pulsewidth > period - 1) pulsewidth = period - 1;
   /* Timer1 fast PWM mode */
   TCCR1A = _BV(WGM11) | _BV(WGM10) | _BV(COM1B1);
   if (frequency > 300.)   /* No prescaling */
      TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
   else         /* 64 prescaling */
      TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10) | _BV(CS11);
   OCR1A = period - 1;
   OCR1B = pulsewidth - 1;
   Serial.print("Frequency ");
   Serial.print((float) clock / period);
   Serial.print(", Duty Cycle ");
   Serial.println(pulsewidth * 100. / period);
   Serial.println();
   delay(100);
   while (!Serial.available()) {
      set_sleep_mode(SLEEP_MODE_IDLE);
      sleep_enable();
      sleep_mode();
      sleep_disable();
   }
   TCCR1A = _BV(WGM11) | _BV(WGM10) | _BV(COM1B1);
   TCCR1B = _BV(WGM13) | _BV(WGM12);   /* Timer stop */
   TCNT1 = 0;            /* Timer value 0 */
   digitalWrite(10, 0);         /* Output 0 */
   Serial.read();
}

Magluvin

  • Hero Member
  • *****
  • Posts: 5884
Re: Arduino signal generator
« Reply #17 on: November 03, 2016, 06:34:32 AM »

ayeaye

  • Hero Member
  • *****
  • Posts: 866
Re: Arduino signal generator
« Reply #18 on: November 06, 2016, 05:19:52 AM »
Yes it's, quite simple. Yet i put it here, because in spite that it's simple, one should have it when one needs it. Signal generator is in fact the only thing i really need Arduino for, so far. Like all i needed for calibrating my oscilloscope, was a multimeter, and a square wave signal generator. Arduino Nano costs in ebay $2.20 with shipping, so makes sense to have it even when using it only as a signal generator. The only problem, it needs a different usb cable than the micro usb cable used for most smartphones and cameras today. It may be used for many special purposes when the need may be, like its analog comparator is fast and enables to measure a changing signals in quite many ways.

The cheaper option may be the Digispark Attiny85 board, which costs only $1.20 in ebay, and is almost the same, except that it has less pins, less memory, and only two 8 bit timers. So the signal generator code has to be somewhat modified for that, though not much, and there is not so much precision for a duty cycle. The problem with that though, is that it uses an internal 16 MHz clock, not an external quartz resonator like Arduino Nano. When using it with usb, the clock is calibrated by usb, and said to have a 1% precision, when the temperature doesn't change much that is, but when using without usb, the precision is only 10%, unless separately calibrated every time. This may be good enough for some purposes but it is not good enough for some measuring.

Magluvin

  • Hero Member
  • *****
  • Posts: 5884
Re: Arduino signal generator
« Reply #19 on: November 06, 2016, 07:17:38 AM »
Just alternative suggestions. Had a TRS-80 color computer back in the days. I believe it was .88mhz cpu and could be over clocked some, like just below or over 1mhz. The processor had to handle all the graphics, of which would take away valuable computing power. Then my brother got a Vic-20 when it came out and it at least had sprites, in which left the cpu more time to do other things while still producing better graphics. Then I got into the Amiga 500, kinda toward the end of their life. But the graphics were incredible, mostly due to separate graphics chips. So now the cpu is able to spend all its time doing other things than producing the graphics display functions.

So I lean toward independent driver circuits when using the arduino so the arduino isnt stressed with avoiding possible little glitches of the desired output when the processor has workloads or even taking time to accept input to adjust the outputs. Anyway

Mags

ayeaye

  • Hero Member
  • *****
  • Posts: 866
Re: Arduino signal generator
« Reply #20 on: November 06, 2016, 06:32:37 PM »
My Arduino Nano. Costs $2.26 with shipping when bought from ebay  http://www.ebay.com/itm/MINI-USB-Nano-V3-0-ATmega328P-CH340G-5V-16M-Micro-controller-board-For-Arduino-N-/141975930194?hash=item210e6cc552:g:e58AAOSwpDdVL5yR . Because it's small, it comes quite fast, and it comes directly to your mailbox, so it's easy to get. If one is very rich, one may think about buying that one for $3.78 with shipping, which also comes with the right usb cable  http://www.ebay.com/itm/ATMEGA328P-FT232RL-FTDI-Micro-controller-Module-for-Arduino-Nano-V3-0-With-Cable-/381801276448?hash=item58e5212420:g:kVMAAOSwHnFVybGk . Though if one happens to have some very old digital camera somewhere or something, then its usb cable is exactly the right one.

ayeaye

  • Hero Member
  • *****
  • Posts: 866
Re: Arduino signal generator
« Reply #21 on: November 06, 2016, 08:45:34 PM »
The Digispark ATtiny85 board costs in ebay $1.52 with shipping  http://www.ebay.com/itm/Digispark-ATTINY85-General-Micro-USB-Development-Board-For-Arduino-New-WA-/281921984524?hash=item41a3dc180c:g:lMEAAOSwXshWqy3A . A link to their license https://creativecommons.org/licenses/by-sa/3.0 . That one has really not much more on it, bout the ATtiny85 integrated circuit, and a voltage regulator, which is also unnecessary when powered by usb. The only thing is that for using a separate ATtiny85 like that, a bootloader has to be written into it, which needs a programmator. This one goes to the standard usb socket, there is also a variety with a micro usb socket on it, but that one is somewhat more expensive. Can be used with the Arduino software, but read above about the limitations of this one.

madddann

  • Full Member
  • ***
  • Posts: 159
Re: Arduino signal generator
« Reply #22 on: May 02, 2018, 01:26:41 AM »
Hello everyone!

I know this thread is old, but since the topic is right, I will post here.
I'm looking for some arduino code, or someone that would be so kind and write it for me and whoever may need it next.
What I'm looking for is a two phase PWM signal generator with:
-variable frequency from 1Hz (or at least around 5Hz) up to whatever is possible - reasonable  (adjustable with pot.)
-variable duty cycle from 1% to 100%, resolution the more the better (adjustable with pot.)
-two outputs shifted (delayed) for half the total period (180°), (not inverted - same signal on both channels only shifted)

Well, that's it. I would try to modify the last code posted by user ayeaye if I only had a clue what is written inthere  :D .
I have an arduino nano 328p, V3.

I'm experimenting with a Figuera type setup, that's what I need the two phase signal generator for.
I rarely post, so I'll understand if this goes unnoticed, but this is the only time in 10 years that I'm asking for something, so please, If someone with the right skills is reading this and willing to help out, I will be very gratefull.

Dann


kEhYo77

  • Full Member
  • ***
  • Posts: 247
Re: Arduino signal generator
« Reply #23 on: May 02, 2018, 06:40:37 AM »
Reposting this from Figuera thread.

All you need is:[/size]1. ONE 10k/100k Ohm potentiometer. Connect the middle leg to Arduino's "A0" analog input. The other two legs of the pot goes to +5V and GND on Arduino.2. TWO Logic Level MOSFET transistors to do the switching (Logic level - like in IRL series -  means that a mosfet is in a conduction saturation state at just +5V put to its gate). Connect the Gate of one mosfet to "Pin 3" and the others' gate to "Pin 11". Sources go to the "GND" of the Arduino board.3. Connect +(positive) from a battery to both "North" & "South" coils and their ends to both drains in the two mosfets and -(negative) to the Arduino's "GND" close to the Source legs of mosfets.4. Connect fast shottky diodes across each coil to do the freewheeling of current.Program description:Arduino is generating a digital signal at 32 kHz frequency using 2 PWM outputs. The value for each "sample" is taken from the sine table. There are 256 values of resolution for the "shape" of the sine wave and 256 values of amplitude. You can change phase shift by changing "offset" variable. Potentiometer allows to set the analog frequency from 0 to 1023 Hz at 1 Hz resolution...NOW copy the code below to Arduino IDE window and save it to the microconroller and HERE YOU GO! 

Quote
/* CLEMENTE FIGUERAS GENERADOR DRIVER
 * modification by kEhYo77
 *
 * Thanks must be given to Martin Nawrath for the developement of the original code to generate a sine wave using PWM and a LPF.
 * http://interface.khm.de/index.php/lab/experiments/arduino-dds-sinewave-generator/
*/




#include "avr/pgmspace.h" //Store data in flash (program) memory instead of SRAM




// Look Up table of a single sine period divied up into 256 values. Refer to PWM to sine.xls on how the values was calculated
PROGMEM  prog_uchar sine256[]  = {
  127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,176,178,181,184,187,190,192,195,198,200,203,205,208,210,212,215,217,219,221,223,225,227,229,231,233,234,236,238,239,240,
  242,243,244,245,247,248,249,249,250,251,252,252,253,253,253,254,254,254,254,254,254,254,253,253,253,252,252,251,250,249,249,248,247,245,244,243,242,240,239,238,236,234,233,231,229,227,225,223,
  221,219,217,215,212,210,208,205,203,200,198,195,192,190,187,184,181,178,176,173,170,167,164,161,158,155,152,149,146,143,139,136,133,130,127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,78,
  76,73,70,67,64,62,59,56,54,51,49,46,44,42,39,37,35,33,31,29,27,25,23,21,20,18,16,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,16,18,20,21,23,25,27,29,31,
  33,35,37,39,42,44,46,49,51,54,56,59,62,64,67,70,73,76,78,81,84,87,90,93,96,99,102,105,108,111,115,118,121,124




};
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) //define a bit to have the properties of a clear bit operator
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))//define a bit to have the properties of a set bit operator




int PWM1 = 11; //PWM1 output, phase 1
int PWM2 = 3; //PWM2 ouput, phase 2
int offset = 127; //offset is 180 degrees out of phase with the other phase




double dfreq;
const double refclk=31376.6;      // measured output frequency
int apin0 = 10;




// variables used inside interrupt service declared as voilatile
volatile byte current_count;              // Keep track of where the current count is in sine 256 array
volatile unsigned long phase_accumulator;   // pahse accumulator
volatile unsigned long tword_m;  // dds tuning word m, refer to DDS_calculator (from Martin Nawrath) for explination.




void setup()
{
  pinMode(PWM1, OUTPUT);      //sets the digital pin as output
  pinMode(PWM2, OUTPUT);      //sets the digital pin as output
  Setup_timer2();
 
  //Disable Timer 1 interrupt to avoid any timing delays
  cbi (TIMSK0,TOIE0);              //disable Timer0 !!! delay() is now not available
  sbi (TIMSK2,TOIE2);              //enable Timer2 Interrupt




  dfreq=10.0;                    //initial output frequency = 1000.o Hz
  tword_m=pow(2,32)*dfreq/refclk;  //calulate DDS new tuning word
 
  // running analog pot input with high speed clock (set prescale to 16)
  bitClear(ADCSRA,ADPS0);
  bitClear(ADCSRA,ADPS1);
  bitSet(ADCSRA,ADPS2);




}
void loop()
{
        apin0=analogRead(0);             //Read voltage on analog 1 to see desired output frequency, 0V = 0Hz, 5V = 1.023kHz
        if(dfreq != apin0){
          tword_m=pow(2,32)*dfreq/refclk;  //Calulate DDS new tuning word
          dfreq=apin0;
        }
}




//Timer 2 setup
//Set prscaler to 1, PWM mode to phase correct PWM,  16000000/510 = 31372.55 Hz clock
void Setup_timer2()
{
  // Timer2 Clock Prescaler to : 1
  sbi (TCCR2B, CS20);
  cbi (TCCR2B, CS21);
  cbi (TCCR2B, CS22);




  // Timer2 PWM Mode set to Phase Correct PWM
  cbi (TCCR2A, COM2A0);  // clear Compare Match
  sbi (TCCR2A, COM2A1);
  cbi (TCCR2A, COM2B0);
  sbi (TCCR2A, COM2B1);
 
  // Mode 1  / Phase Correct PWM
  sbi (TCCR2B, WGM20); 
  cbi (TCCR2B, WGM21);
  cbi (TCCR2B, WGM22);
}








//Timer2 Interrupt Service at 31372,550 KHz = 32uSec
//This is the timebase REFCLOCK for the DDS generator
//FOUT = (M (REFCLK)) / (2 exp 32)
//Runtime : 8 microseconds
ISR(TIMER2_OVF_vect)
{
  phase_accumulator=phase_accumulator+tword_m; //Adds tuning M word to previoud phase accumulator. refer to DDS_calculator (from Martin Nawrath) for explination.
  current_count=phase_accumulator >> 24;     // use upper 8 bits of phase_accumulator as frequency information                     
 
  OCR2A = pgm_read_byte_near(sine256 + current_count); // read value fron ROM sine table and send to PWM
  OCR2B = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset)); // read value fron ROM sine table and send to PWM, 180 Degree out of phase of PWM1
}

http://www.youtube.com/watch?v=hC70s3tYaGs&hd=1

madddann

  • Full Member
  • ***
  • Posts: 159
Re: Arduino signal generator
« Reply #24 on: May 02, 2018, 11:37:04 AM »
Wow! Thank you kEhYo77, now i remember seeing your video... long time since then...
The code you posted will sure come handy in the future, but what I need right now is much simpler, I knew I forgot to mention something.

What I'm asking for is just two square wave signals offset by half the period (180°), with adjustable on time and frequency, thats all.

Like the two signals "PWM1" and "PWM3" on this picture: https://batchloaf.files.wordpress.com/2015/03/four_phase_pwm.png

I'm doing Figuera experiments "Tesla style" - creating two resonant tanks with oposite signals - wich the electromagnets are a part of.
I'm sure you could try this also on your setup (from the video)... just add capacitors to the coils (electromagnets) and excite the hell out of it  ;D .

Thanks again! If you or anyone else would put together the code for the two square wave signals, that would be great.

Dann



kEhYo77

  • Full Member
  • ***
  • Posts: 247
Re: Arduino signal generator
« Reply #25 on: May 02, 2018, 11:58:57 AM »
Maybe just this.

madddann

  • Full Member
  • ***
  • Posts: 159
Re: Arduino signal generator
« Reply #26 on: May 02, 2018, 06:44:16 PM »
Well, yes that chip would be perfect for what I need, but I just spent the last 3 hours trying to get it to work with no success.
I have only one chip and that one may be defective. A few times I could see some signal for a fraction of a second when connecting the supply voltage. I could also see the ramp waveform on the capacitor, but other than that there were no other pulsed signals on any other pin.
Did you test your schematic? Was it working for you?
Thanks again kEhYo77, if you happen to think of any alternatives, I'm all ears.

Dann

kEhYo77

  • Full Member
  • ***
  • Posts: 247
Re: Arduino signal generator
« Reply #27 on: May 02, 2018, 08:54:32 PM »
Yes. I have tested it and it is working for me.

gyulasun

  • Hero Member
  • *****
  • Posts: 4117
Re: Arduino signal generator
« Reply #28 on: May 02, 2018, 11:29:16 PM »
Well, yes that chip would be perfect for what I need, but I just spent the last 3 hours trying to get it to work with no success.
I have only one chip and that one may be defective. A few times I could see some signal for a fraction of a second when connecting the supply voltage. I could also see the ramp waveform on the capacitor, but other than that there were no other pulsed signals on any other pin.
Did you test your schematic? Was it working for you?
Thanks again kEhYo77, if you happen to think of any alternatives, I'm all ears.

Dann

Hi,

Sorry to chime in,  this TL494 circuit was a topic on this forum, and member itsu for instance
built the circuit and it also worked for him, see this link with his schematics:
http://overunity.com/12736/kapanadze-cousin-dally-free-energy/msg507286/#msg507286

His circuit is almost the same: the difference is he connected pins 14 to pin 15 and grounded
pin 16,  and he added decoupling capacitors as you can see.
I am not saying the schematic by kEhYo77 is not working, perhaps the trick is the use of the
filter capacitors across the 5V reference and ground and also across the main supply input and ground.
IT would be worth trying these changes.

And here is yet another variant where the dead time can also be varied, besides duty cycle and
frequency:  http://overunity.com/12736/kapanadze-cousin-dally-free-energy/msg502495/#msg502495
Of course your TL494 might be defective.

Gyula

madddann

  • Full Member
  • ***
  • Posts: 159
Re: Arduino signal generator
« Reply #29 on: May 03, 2018, 10:21:03 PM »
Hello Gyula!

Don't be sorry, any helpful input is well appreciated. I put together the circuit from the schematic by Itsu an it works, but the output signals are kinda jumpy at times, like when I turn the duty cycle pot to a certain value (the pots are new). It's also starnge that when the duty cycle is in the region near max. everything looks good, but when I turn the pot down slowly and evenly, the duty cycle on one output channel (I think pin 9) jumps to minimum and stays there until I turn the pot back up near max. I also played with separate duty and dead time circuit but it's the same story. Tryed two equivalent chips (KA7500B) and still all the same.

The schematic from kEhYo77 works only if I connect pins 1 and 16 to ground.

But maybe I shoudn't rely on cheap chinese protoboards for testing circuits.

Thanks guys!


Dann