Hi there,
I'm using a Teensy 4.1 combined with this PWM driver by Adafruit
https://learn.adafruit.com/16-channel-pwm-servo-driver?view=all
The issue I'm experiencing, is that when I try to use the NativeEthernet library combined with the PWMServoDriver library, the moment I call Ethernet.begin(); one or two seconds later the PWM driver stops responding
Below is the example patch I'm using, its basically just the pwmtest example from the PWMServoDriver library and the UDPSendReceiveString example from the NativeEthernet library.
Does anyone know why this might be happening? Are there any reserved pins or other things that are interfering? I'm running the example on a custom PCB, I'll try and setup the same experiment with just a teensy and one of Adafruit's boards to see if it makes a difference, I'm quite puzzled though, since as far as I understand the PWM library used I2C, and the NativeEthernet library uses SPI, correct?
I'm using a Teensy 4.1 combined with this PWM driver by Adafruit
https://learn.adafruit.com/16-channel-pwm-servo-driver?view=all
The issue I'm experiencing, is that when I try to use the NativeEthernet library combined with the PWMServoDriver library, the moment I call Ethernet.begin(); one or two seconds later the PWM driver stops responding
Below is the example patch I'm using, its basically just the pwmtest example from the PWMServoDriver library and the UDPSendReceiveString example from the NativeEthernet library.
Does anyone know why this might be happening? Are there any reserved pins or other things that are interfering? I'm running the example on a custom PCB, I'll try and setup the same experiment with just a teensy and one of Adafruit's boards to see if it makes a difference, I'm quite puzzled though, since as far as I understand the PWM library used I2C, and the NativeEthernet library uses SPI, correct?
Code:
/***************************************************
This is an example for our Adafruit 16-channel PWM & Servo driver
PWM test - this will drive 16 PWMs in a 'wave'
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/815
These drivers use I2C to communicate, 2 pins are required to
interface.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include <NativeEthernet.h>
#include <NativeEthernetUdp.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 2, 177);
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#define PWM_NUM 8
Adafruit_PWMServoDriver pwm[] = { (0x40), (0x41), (0x42), (0x43), (0x44), (0x45), (0x46), (0x47) };
// called this way, it uses the default address 0x40
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// you can also call it with a different address and I2C interface
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40, Wire);
void setup() {
Serial.begin(9600);
Serial.println("16 channel PWM test!");
// start the Ethernet
Ethernet.begin(mac, ip);
for (uint8_t p = 0; p < PWM_NUM; p++) {
pwm[p].begin();
/*
In theory the internal oscillator (clock) is 25MHz but it really isn't
that precise. You can 'calibrate' this by tweaking this number until
you get the PWM update frequency you're expecting!
The int.osc. for the PCA9685 chip is a range between about 23-27MHz and
is used for calculating things like writeMicroseconds()
Analog servos run at ~50 Hz updates, It is importaint to use an
oscilloscope in setting the int.osc frequency for the I2C PCA9685 chip.
1) Attach the oscilloscope to one of the PWM signal pins and ground on
the I2C PCA9685 chip you are setting the value for.
2) Adjust setOscillatorFrequency() until the PWM update frequency is the
expected value (50Hz for most ESCs)
Setting the value here is specific to each individual I2C PCA9685 chip and
affects the calculations for the PWM update frequency.
Failure to correctly set the int.osc value will cause unexpected PWM results
*/
pwm[p].setOscillatorFrequency(27000000);
pwm[p].setPWMFreq(1600); // This is the maximum PWM frequency
}
// if you want to really speed stuff up, you can go into 'fast 400khz I2C' mode
// some i2c devices dont like this so much so if you're sharing the bus, watch
// out for this!
Wire.setClock(400000);
}
void loop() {
float b = map(sin(millis() * 0.01), -1, 1, 0, 1);
int t = b * 4095;
for (uint8_t c = 0; c < PWM_NUM; c++) {
for(uint8_t p = 0; p < 16; p++) {
pwm[c].setPWM(p, 0, t );
}
}
}