Show Posts
Pages: 1 2 3 [4] 5 6 ... 9
46  Using Arduino / Motors, Mechanics, and Power / Re: Steeper motor control with a dewll time on: August 03, 2014, 07:00:55 pm
Something like this?

byte stepPin = 6;
byte dwellControlPin = 5;

unsigned numberOfSteps = 17;
unsigned dwellTimeMillis = 30000;
unsigned timeBetweenPosn = 2 * 1000;  // millis

unsigned timeBetweenSteps = timeBetweenPosn / numberOfSteps;

void moveSteps(int numSteps) {
   for (int n = 0; n < numSteps; n++) {
       digitalWrite(stepPin, HIGH);
       digitalWrite(stepPin, LOW);
       delay(timeBetweenSteps); // controls motor speed

void setup() {
  pinMode (stepPin, OUTPUT);
  Serial.begin (115200);

void loop() {
   // get the dwell time
   dwellTimeMillis = (int)(analogRead (dwellControlPin) * 30000.0 / 1023.0);
   Serial.println (dwellTimeMillis);
47  Development / Suggestions for the Arduino Project / Re: Arduino Board with Parallax Multi-Processor Chip on: August 02, 2014, 02:37:48 pm
I find it interesting, from the novelty side at least. I am still worried about the lack of real peripherals, what happens when I want 16 peripherals? I haven't counted, but for a sizeable project that must be a limit. Having said that, I would quite like a dedicated "stepper driver peripheral".

Parallax do an Arduino format board for their current Propeller chip, I guess they would do one for their Propeller 2 chip. So maybe the hardware side is already covered, although I think the hardware is the easy bit.

The hard bit I think would be fitting the Propeller architecture into Arduino IDE. I guess that the IDE can launch a compiler and download to unusual devices fairly easily, but a lot of what people mean by Arduino ease of use is in the libraries - serial, io, analog, wire, spi etc. I guess those all require a complete rewrite for Propeller, which could be a big job.

I see that Parallax also have an IDE, I guess it can download as well. That kinda raises the question, why not use the Propeller IDE for programming it?
48  Products / Arduino Due / Re: What are the possible PWM frequencies? And how to set them. on: August 02, 2014, 01:23:49 pm
As promised here is a sketch using timers for PWM, with different frequencies. You can have 4 different frequencies, because there are 4 timer channels used. Each timer channel can have two outputs, A and B, each with their own duty cycles.

There are a total of 9 timer channels, so in theory you could have 9 different frequencies, but not all the output pins may be mapped in convenient locations, or at all. You would also need to do all the setup code yourself, as they are not handled by analogWrite, or possibly edit variant.cpp.

// --------------------------------------------
// PWM with timers demo
// Bob Cousins, August 2014
// --------------------------------------------

typedef struct {
    Tc *pTC;        // TC0, TC1, or TC2
    byte channel;   // 0-2
    byte output;    // 0 = A, 1 = B
}  tTimerInfo;

tTimerInfo timerLookup [] =
  {NULL,0,0}, // 0
  {TC0,0,0},  // pin 2 = TIOA0
  {TC2,1,0},  // pin 3 = TIOA7
  {TC2,0,1},  // pin 4 = TIOB6
  {TC2,0,0},  // pin 5 = TIOA6
  {TC2,1,1},  // pin 10 = TIOB7
  {TC2,2,0},  // pin 11 = TIOA8
  {TC2,2,1},  // pin 12 = TIOB8
  {TC0,0,1}   // pin 13 = TIOB0

 * \brief set a pin for PWM using a timer channel
 * \param pin       pin to use (0-13 only!)
 * \param frequency the frequency
 * \param dutyCyle  duty cycle 0-255
 * \return          this function returns a count, which is the effective PWM resolution. Returns 0 if pin is not valid

uint32_t setupTimerPwm (byte pin, uint32_t frequency, unsigned dutyCycle)
  uint32_t count = VARIANT_MCK/2/frequency;
  tTimerInfo *pTimer = &timerLookup[pin];
  if (pTimer != NULL)
    TC_SetRC (pTimer->pTC, pTimer->channel, count);
    if (pTimer->output == 0)
       TC_SetRA (pTimer->pTC, pTimer->channel, count * dutyCycle / 256);
       TC_SetRB (pTimer->pTC, pTimer->channel, count * dutyCycle / 256);
    return count;
    return 0;

void setup() {
  // put your setup code here, to run once:

  // use the Arduino lib to do initial setup
  analogWrite (2, 128);
  analogWrite (13, 128);
  analogWrite (5, 128);
  analogWrite (4, 128);

  analogWrite (3, 128);
  analogWrite (10, 128);

  analogWrite (11, 128);
  analogWrite (12, 128);
  // pins 2 and 3 share the same timer so must have same frequency
  setupTimerPwm (2, 2000, 128);
  setupTimerPwm (13, 2000, 64);
  // pins 5 and 4 share the same timer
  setupTimerPwm (5, 3000, 128);
  setupTimerPwm (4, 3000, 64);

  // pins 3 and 10 share the same timer
  setupTimerPwm (3, 4000, 128);
  setupTimerPwm (10, 4000, 64);

  // pins 11 and 12 share the same timer
  setupTimerPwm (11, 5000, 128);
  setupTimerPwm (12, 5000, 64);


void loop() {
  // put your main code here, to run repeatedly:


Scope traces, showing pins 2, 5, 3 and 11 respectively (these are the 'A' outputs) :

demo2 by donotdespisethesnake, on Flickr

If you find the advice "read the source code" offensive, I'm sorry, but please don't send me PMs to complain. It really is Good Advice Learn to Read the Source, Luke!. If you don't want to read source code, consider a different hobby/profession!

And if you are using Arduino, you already have it's source code on your computer, so don't say you don't have it. It is really worth studying if you want to learn how it works.
49  Development / Other Hardware Development / Re: My new Board on: August 02, 2014, 11:02:48 am
That's about what I would have guessed. I was wondering because I am looking at cheap 3D printer controllers, which are slowly moving from AVR to ARM.

The SAM3X8E is about £8, for about £9 I could get a Cortex A5 (e.g. SAMA5), which is a significant bump in performance for a small fraction of the overall cost. It could also run Linux, with external RAM.

Finding non-BGA packages for the bigger chips limits the options though, so a fast M3 or M4 is probably more practical. A SAM3X board has the advantage of the Arduino ecosystem of course.
50  Products / Arduino Due / Re: (Trying to) develop a speed servo using Arduino due & arduino motor shield R3 on: August 02, 2014, 05:44:45 am
That looks great!

In software, the technique is normally referred to as Finite State Machine (FSM), in computer science courses they may refer to Finite State Automaton (FSA), but FSM is the general usage.

Your robot for kids reminds me of Logo
51  Products / Arduino Due / Re: What are the possible PWM frequencies? And how to set them. on: August 02, 2014, 05:31:11 am
Here is a demo sketch:

// --------------------------------------------
// Hardware PWM with different frequencies demo
// Bob Cousins, August 2014
// --------------------------------------------

void setup() {
  // put your setup code here, to run once:

  pinMode (6, OUTPUT);  // PWML7
  pinMode (7, OUTPUT);  // PWML6
  pinMode (8, OUTPUT);  // PWML5
  // call analogWrite first to allow Arduino to initialise  
  analogWrite (6, 128);
  analogWrite (7, 128);
  analogWrite (8, 128);
  // set clka to 10KHz, clkb to 5kHz
  PWMC_ConfigureClocks (255 * 10000, 255 * 5000, VARIANT_MCK);
  // set channel 6 to use clkb
  PWMC_ConfigureChannel (PWM, 6, PWM_CMR_CPRE_CLKB, 0, 0);
  // Note: need to re-enable channel after doing Configure
  PWMC_EnableChannel (PWM, 6);
  // set channel 5 to use MCK/16 (actual freq = 84MHz/255/16 = 20588 Hz)
  PWMC_ConfigureChannel (PWM, 5, PWM_CMR_CPRE_MCK_DIV_16, 0, 0);
  PWMC_EnableChannel (PWM, 5);

void loop() {
  // put your main code here, to run repeatedly:


And scope trace

demo_pwm by donotdespisethesnake, on Flickr
52  Products / Arduino Due / Re: What are the possible PWM frequencies? And how to set them. on: August 02, 2014, 04:48:57 am
But ....
The PWMC_ConfigureClocks method is defined in [arduino IDE location]/hardware/arduino/sam/system/libsam/include/pwmc.h.
So it is a amtel delivered method implemented in compiled code (so it is "proprietary code")
I however fail to find any documentation on  PWMC_ConfigureClocks.
It's code provided by Atmel as part of their ASF (Atmel Software Framework). It has a BSD style license, so there is no problem to include with open source code.
There is no deeper level of documentation than the doxygen comments in the code, apart from example progams. Atmel have the documentation online, but note the Atmel code used by Arduino is now out of date with respect to the latest ASF.

pwmc.h full documentation on the method is:
*    -# Configures PWM clocks A & B to run at the given frequencies using
 *       \ref PWMC_ConfigureClocks().
The definition is
void PWMC_ConfigureClocks(uint32_t clka, uint32_t clkb, uint32_t mck ) ;
I found nothing in pwmc.h that something should be set before calling this method. The arduino code does state you should call analogwrite before the call.

The great thing about software is that a complete specification for what the software does is the software itself smiley The compiler must be able to compile without reference to further documentation. It really is an essential skill for anyone writing code to be able to read code as well.

If you are not sure what the registers and values mean, then reading the SAM3X datasheet is a complete and accurate specification. There is no point me copying and pasting the SAM3X datasheet, you can read it for yourself!

In this case, it is obvious from reading the code that analogWrite calls PWMC_ConfigureClocks the first time it is called, which would overwrite any previous initialisation. I wouldn't expect that to be documented anywhere, it's just simple programming logic.

So maybe the code is setting the pwm frequency for all pwm pins? Can I call this multiple times with different frequencies?

Each channel can select from a number of clock sources, the way Arduino sets up is to use the same source for all channels (clka). PWMC_ConfigureClocks() can set clka and clkb, so that gives you two programmable clock sources. The other clock sources are divisors of the main clock, as described in the SAM3X datasheet (p1058)

CPRE: Channel Pre-scaler
Value Name Description
0b0000 MCK Master clock
0b0001 MCK_DIV_2 Master clock/2
0b0010 MCK_DIV_4 Master clock/4
0b0011 MCK_DIV_8 Master clock/8
0b0100 MCK_DIV_16 Master clock/16
0b0101 MCK_DIV_32 Master clock/32
0b0110 MCK_DIV_64 Master clock/64
0b0111 MCK_DIV_128 Master clock/128
0b1000 MCK_DIV_256 Master clock/256
0b1001 MCK_DIV_512 Master clock/512
0b1010 MCK_DIV_1024 Master clock/1024
0b1011 CLKA Clock A
0b1100 CLKB Clock B

Anyone knows a pointer to documentation of PWMC_ConfigureClocks?

You have already found it!

how do I set PWM pin 2 to 10KHz and pin 13 to 44Khz

So far I have been describing the hardware PWM channels, and these are available on pins 6,7,8,9. There are other hwPWM channels, but they are not mapped in Arduino. You would either need to change variant.cpp, or copy the PWM code from analogWrite.

If you look at the Due pinout diagram, if the SAM pin name is "PWMxx" then it's a hardware PWM channel. There are 8 channels, each with a "high" and "low" output. Therefore there are PWML0, PWMH0, etc up to PWML7, PWMH7. Each channel can have a different clock source, which will affect both high and low outputs.

The other pins marked "PWM" in the Due pinout i.e. 2-5 and 9-13, are implemented using timer channels. The timer channels are also capable of producing PWM waveforms, without some of the more advanced PWM features, but can produce more accurate timing.

I will have a look at those and put an example in another post.
53  Products / Arduino Due / Re: What are the possible PWM frequencies? And how to set them. on: August 01, 2014, 02:10:40 pm

There are 11 (or 18) pwm pins. It can't be that hard to list the resources needed to  set the pwm frequency for those pins?

It's not too hard, but it's long : it's all in the SAM3X datasheet, 1455 pages. Creating a short summary of all that info is hard though. The datasheet does contain the quote "The large number of source clocks can make selection difficult." smiley But often you don't need to worry too much about the details in the datasheet, just use the library functions Atmel provide.

Really, I usually find reading the library source code is the best way to figure out how to do things. For example, to set the PWM clock, just call PWMC_ConfigureClocks(uint32_t clka, uint32_t clkb, uint32_t mck) and let the function work out what all the registers should be. There is an example in analogWrite, which was referenced in the other thread.

It's complicated slightly because the Arduino library presents pins as "PWM enabled", but under the hood does things differently depending on the hardware.

A 42MHz PWM is probably a little ambitious, for a typical 8 bit PWM, the max freq would be 84MHz/256.

Here is a simple example I just tried for a real PWM pin, to generate 10kHz, 50% duty cycle, using default 8 bit resolution:

void setup() {
  // put your setup code here, to run once:

  pinMode (6, OUTPUT);
  analogWrite (6, 128);
  // must be called after first analogWrite
  PWMC_ConfigureClocks (256 * 10000, 0, VARIANT_MCK);

void loop() {
  // put your main code here, to run repeatedly:


And here is what you get on the scope:

54  Development / Other Software Development / Re: Language-reference - please include more details on the internals. on: August 01, 2014, 11:40:32 am
The thing is, 99.99% of the details you never need to know. That's the way technology is organised, and just as well, otherwise we would never be able to write a single line of code, if we had to know the operation of every gate inside the CPU. There is probably no one in Atmel who knows all that.

Similarly, the GCC compiler is over 7 million lines of code, I would strongly suggest not to learn the inside details!

The best way to learn about the source code is to read the source code. And the best way to improve it is to submit patches smiley

55  Development / Other Hardware Development / Re: My new Board on: August 01, 2014, 08:55:53 am
Looks very nice!

Any idea of cost?
56  Products / Arduino Due / Re: Problems programming the Due on: August 01, 2014, 08:49:21 am
Paul's makefile is good, but it doesn't build libsam, it uses the one from the Arduino IDE, although it does build core.a.

I guess if you don't need to change any libsam files it's not a problem.
57  Products / Arduino Due / Re: some questions on due pwm on: August 01, 2014, 02:45:32 am
I monitor an input line. As soon as it goes up, I wait for a predetermined amount of time and then raise an output line for another different predetermined amount of time. I need to do this for 8 output lines and I need to do it fast.

That sounds like it is ideally suited for timer channels. PWM is normally used for continuous output.

Although depending on what "fast" means, it might be better to do in hardware.
58  Products / Arduino Due / Re: error compiling on: July 31, 2014, 04:00:47 pm
Here you go!

Arduino: 1.5.7 (Windows 7), Board: "Arduino Uno"

C:\Program Files (x86)\Arduino/hardware/tools/avr/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=157 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard C:\Users\Marty\AppData\Local\Temp\build1208768339966323662.tmp\Blink.cpp -o C:\Users\Marty\AppData\Local\Temp\build1208768339966323662.tmp\Blink.cpp.o

Looks just like the stuff posted previously.

Ah, ok! I was expecting more output because there doesn't seem to be any error in there, it looks like a normal compile command.

So perhaps that means that the IDE is not capturing the error. it would be useful to know what it is so the output could be improved. Catch 22!

If you have "Show verbose output during compilation" already switched on (which I think you do), I'm not sure if there is any other logging.
59  Products / Arduino Due / Re: error compiling on: July 31, 2014, 01:20:37 pm
I don't think empty libraries makes a difference as I already tried a run with the hardware folder renamed to oldhardware and libraries folder renamed to oldlibraries. When I brought up the IDE it created a new folder called libraries with a read me.txt file in it.

The compile fails fairly quickly on avr devices but not due. So is there some debugging I can do besides turning on verbose. As I understand the IDE is issuing the equivalence of command lines to run the compile so is it possible I could run first couple of commands manually to try to catch the failure. Or better yet is there any kind setting file that will allow some debugging setting.

Hey, press the button marked "Copy error messages" - that's what it's for! Then paste here....
60  Products / Arduino Due / Re: 3.3 volt I/O pins on: July 31, 2014, 01:12:37 pm

I am using an arduino Due to communicate with sensor that uses SDI12 protocol.
I have found SDI12 library for Arduino on github.
However, because SDI12 low and high voltage ranges between 0-5V, how should I connect it with the due?

Thanks.  smiley

Well, Firefox just lost my reply, so here goes again:

Unfortunately, SDI signal levels are not compatible with 3.3V, which is a shame because otherwise it is a simple protocol.
You will need a circuit to do some bidirectional level shifting e.g.

You may need to make some changes to the SDI libray to toggle the direction control.
Pages: 1 2 3 [4] 5 6 ... 9