Introduction
This post it is shown how to use the pulses module on Tools40 for Arduino based PLC.
Requirements
Ethernet or 20 I/Os PLC:Â Â Â Â Ethernet PLCÂ Â Â Â Â Â Â 20 I/Os PLCÂ Â Â Â
Industrial Shields boards:Â Â Â Industrial Shields Boards
Tools40 library: Â Â Â Â Â Â Â Â Â Â Tools40Â
Description
Pulses module library is a library inside of Tools40 make it for Industrial Shields developers to use pulses or square waveforms (SQW) on the PWM Pins of  Industrial Shields equipment’s.Â
The startPulses(Pin, Frequency, Precision)
 the function starts the train of pulses at the specified frequency and precision. The default frequency is 1kHz and the default precision is 3.
pinMode(3, OUTPUT);
startPulses(3, 2000, 3);
The stopPulses
 the function stops the train of pulses.
stopPulses(3);
Ardbox Analog HFÂ Â Â Â Â Â Â Â Â Â Â 
On ARDBOX Analog it is possible to use this functions in outputs:
TIMER0: Q0.1 and Q0.6
TIMER1: Q0.2 and Q0.3
TIMER3: Q0.5
M-Duino          
On MDUINO-21, MDUINO-42 and MDUINO-58 it is possible to use these functions in outputs:
TIMER0: Q0.5 and Q2.6
TIMER1: Q2.5 – TIMER2: Q1.5 (Multiply the frequency x2)
TIMER3: PIN2, PIN3 and Q0.6
TIMER4: Q0.7, Q1.6 and Q1.7
TIMER5: Q1.3, Q1.4 and Q2.0
IMPORTANT:Â It is not possible to have different frequencies between the same Timer Pins. Some outputs share the same timer, so they work at the same frequency.
CAUTION!!! When the Timer0 pins are used, all the time functions change their functionality as delay()
, millis()
,micros()
,delayMicroseconds()
 and others.
Next it is showed recommended precision between different frequencies:
Precision = 1: from 30Hz to 150Hz
Precision = 2: from 150Hz to 500Hz
Precision = 3: from 500Hz to 4kHz
Precision = 4: from 4kHz to 32kHz
Precision = 5: from 32kHz to 4MHz
To have a HIGH precision on the desired frequency, it is recommended to use the closer precision to the values of the previous table.
Example
// Pulses library example
//by Industrial Shields
#include <Pulses.h>
//Output pin
//Only pins which an associated timer can be used
//IMPORTANT: when using TIMER0 pins, time related functions (millis, delay...)
//lose their time reference and don't work properly
const int pin=3;
//Frequency in Hz
//Default value: 1000
//NOTE: Use "unsigned long" and "UL" suffix for higher frequencies
const unsigned long frequency = 500UL; //500Hz
//Frequency precision
//Default value: 3
//Recommended usage
//precision=1 from 30Hz to 150Hz
//precision=2 from 150Hz to 500Hz
//precision=3 from 500Hz to 4KHz
//precision=4 from 4KHz to 32KHz
// precision = 5: from 32kHz to 4MHz const int precision = 4;
/////////////////////////////////////////////////////////////
void setup {
   //Set pin as OUTPUT
pinMode(pin, OUTPUT);
//Start train pulses
startPulses(pin, frequency, precision);
}
/////////////////////////////////////////////////////////////
void loop {
}
/////////////////////////////////////////////////////////////
Pulses Module Library on Tools40 for Arduino based PLCs