<< return to Pixycam.com

Program for transforming X and Y positions to two analogue voltages via an Arduino UNO

Hello everbody,

I wrote a program to export the X and Y position of an object with an arduino uno. The X and Y positions are transformed to two PWM-signals. These PWM-signals can be transformed by a simple RC-filter (1000 ohm; 4,7µF). I am not sure if the R and C-values are optimal. Still have to test this RC-filter (tried with 10000 ohm and 10µF but than the transient time is a little long). These analogue voltages are suited to be used by a PLC (Programmable Logic Controller). In the attachment you can find a picture that explains the RC-filter.

I made two programs: “pixy_1_object.ino” and “pixy_2_objects.ino”

In “pixy_1_object.ino” I assume pixy is trained to detect an object of one specific colour. And I also assume there is no more than one such object in the image. If such and object is detected pin 8 is high and the position is outputted as a PWM-signals at pin 3 and 5. This program has been tested and works.

In “pixy_2_objects.ino” I assume pixy is trained to detect objects of two specific colours. And I also assume there are no more than two such objects in the image (one of each colour). If such and object is detected the position is outputted as a PWM-signals at pin 3 and 5 and
a digital output (8 or 9) tells what object it is (what coloursignature). I have not been able to test this program fully.

I noticed that it takes about half a second for the analogue values to change when objects are moved. This is a little bit slow and due to the “slow” arduino uno. It would be interesting to use an arduino due and export the X and Y position directly as analogue voltages (at DAC0 and DAC1). Unfortunately the Pixy.h class doesn’t work with the arduino due (has to do with the SPI-communication). Should anybody solve this problem, please let me know.

Have fun with Pixy,
Bart

I looked at the values in your RC filter, you are in the right ballpark.
Uno and similar boards, pins 5 and 6 have a PWM frequency of approximately 980 Hz.

Your code is slow because the PWM is only updated once every 50 times through the loop.

i++;
if (i%50==0){
  analogWrite();
}

You probably only want the serial.print stuff inside the “if (i%50==0)” block. The code was written this way to avoid spewing too much data out of the serial port.
If you move the PWM code out to the “if (blocks){}” section, it will run much faster.

Hello Hack Bros,

You are right, that code is slow. And I already modified it so that Pixy can be used for this schoolproject (see also the attached file “FastVersionDelayUNO.ino”):

Now it is lightning fast and for the RC-filter I now use 4700 ohm resistor en 4,7µF capacitor which gives transient of 0,022s which is more than good enough. The downside is ofcourse that I can only detect one object but that is okay for our schoolproject.

In the attached program “FastVersionDelayUNO.ino” you will also see a line “TCCR0B = TCCR0B & 0b11111000 | 0x01;”. This sets the PWM-frequency at 62500Hz so that the RC-filter tranforms the PWM to a nearly perfect analogue value ranging from 0,1V to 4,9V. Take notice that delay is not 640ms at the end but 10ms (at 62500Hz a delay of 64000 is 1 second). You can find more information about this here:

http://playground.arduino.cc/Main/TimerPWMCheatsheet

Also here you can see a test setup to see how fast the tracking is:

Greetings,
Bart

Thanks for the video, cool cool cool!

Hi

i was trying the nice FastVersionDelayUNO.ino
but not working with me , am not using the RC-filter , cuz am using digital servo , do i need RC-filter for digital servo HS-5645MG

am working on transforming X and Y positions to four PWM
X positions to two PWM
Y positions to two PWM

can you help

Have a nice day
Best,

Erdal INCI