Hi;
I have successfully built a collision avoidance robot using an Arduino Nano AtMega328, HC-SR04 Sensors, 9gMicro Servo (for scanning) and 2 x DC Motors connected via an L298N Motor Driver.
I am now trying to add a raspberry Pi connected via i2C to control the motors through the Nano, run a Webcam and stream video to a web page that will also be used to control the robot.
I have successfully interfaced the Arduino as Slave using i2C and have managed to blink the onboard LED using python on the raspberry Pi.
I have also managed to (finally) spin the motor connected to the L298N again using a Python command on the Rapsberry Pi.
At first I was having no joy but found that this was because I had been using some of the the Nano PWM as OUTPUTS, when using Sketch without the Pi via i2C on the original bot this was not a problem but when using i2c it seems it may be?
What I am having trouble with is using PWM commands and reading pin status (I keep getting a key error).
My Fault Finding
When I connected up an LED using a 1K resistor connected to pin 3 on the Nano, the LED blinked very momentarily and very dim. I then tried a 470ohm resister and got the same results.
When I looked at the pin status being returned, instead of High it showed a status of P?
Then I checked the pinouts again for the Nano, looking at this page:
http://eezone.co.uk/blog/arduino/arduino-compatible-nano-board-free-usb-cable-2.html
Pin 3 is also PWM so maybe setting the status of this pin to OUTPUT and High makes it PWM only??
I then amended the code and changed the wiring to use non PWM pins for the L298N inputs and a PWM for the L298N en pin.
First I tried using a PWM command with no joy, then I tried just to set the L298N en pin to High and when I ran the code Ta Da, the motor span
So maybe when in i2C mode you can't use PWM pins as standard OUTPUTS to the 'in' pins on the L298N??
I still don't know the correct command for PWM to control the speed???
Here was my code not using PWM using pins that had worked fine on the avoidance bot:
Pinouts for the code below are:
Nano - L298N
08 - in1
07 - in2
11 - enA
There are two motors connected to pin 1 and 2 / 13 and 14
setPin(33, "08", "Output") #on Arduino with I2C ID #33 set pin 08 to OUTPUT
setPin(33, "07", "Output") #on Arduino with I2C ID #33 set pin 07 to OUTPUT
setPin(33, "11", "Output") #on Arduino with I2C ID #33 set pin 11 to OUTPUT
writePin(33,"08", "Low") #on Arduino with I2C ID #33 set pin 08 LOW
writePin(33,"07", "High") #on Arduino with I2C ID #33 set pin 07 HIGH
writePin(33,"11", "High") #on Arduino with I2C ID #33 set pin 11 HIGH
# analogWritePin(33, "11", 245): #on Arduino with I2C ID #33 set PWM on pin 11 to 245
And here is the same code but this time trying to use a PWM analogueWritePin command:
setPin(33, "08", "Output") #on Arduino with I2C ID #33 set pin 08 to OUTPUT
setPin(33, "07", "Output") #on Arduino with I2C ID #33 set pin 07 to OUTPUT
setPin(33, "11", "Output") #on Arduino with I2C ID #33 set pin 11 to OUTPUT
writePin(33,"08", "Low") #on Arduino with I2C ID #33 set pin 08 LOW
writePin(33,"07", "High") #on Arduino with I2C ID #33 set pin 07 HIGH
# writePin(33,"11", "High") #on Arduino with I2C ID #33 set pin 07 HIGH
analogWritePin(33, "11", "245"): #on Arduino with I2C ID #33 set PWM on pin 11 to 245
I then changed the pins for one motor and used:
Nano - L298N
4 - in1
7 - in2
3 - enA
By amending the first set of code above (setting pin 3 High and not using PWM) the motor span, trying the second set of code, again amended for the above pin changes and using a PWM command the motor failed to spin.
I am also still getting pin status read error: