Read PWM Signal from Pixhawk to Arduino - arduino

I tried to read a PWM signal from Pixhawk by connecting it to Arduino.
The Pixhawk is connected to FrSky X8R. But my PWM always reads to zero.
Here's my arduino code
#define RCPin 4
int RCValue;
void setup() {
Serial.begin(9600);
pinMode(RCPin, INPUT);
}
void loop() {
RCValue = pulseIn(RCPin, HIGH,25000);
Serial.println(RCValue);
}
The picture attached is also my wiring.

First of all pin 4 isn't a PWM pin. As i can see you're using Arduino UNO. In UNO PWM pins are: 3, 5, 6, 9, 10, 11
And their Frequencies: 490 Hz except pin 5 and pin 6, their frequencies is: 980 Hz.
So, change your pin as 5 and it will work then.

Related

Wrong character output from I2C LCD with Arduino

I'm just trying to print a string onto an LCD display (SparkFun 20x4 SerLCD - RGB Backlight Qwiic). The address of the I²C device is 0x72. I'm using an Arduino Teensy to communicate with the LCD display.
Pin Connections:
LCD - Arduino
GND - GND (pin 2)
RAW - 3.3V (pin 3)
SDA - A4 / SDA0 (pin 18)
SCL - A5 / SCL0 (pin 19)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int LCD_columns=20;
int LCD_rows=4;
LiquidCrystal_I2C lcd(0x72,LCD_columns,LCD_rows); // Address, Columns, Rows
void setup(){
Wire.setClock(10000);
lcd.init();
lcd.setCursor(0, 0);
lcd.print("2");
}
void loop()
{
}
In the attached image lcd.init() just prints the characters up to the 11!! and when the lcd.print("2") is used it printed the random characters and also the 11!!. I'm not sure where to go from here, any help is greatly appreciated. Image of LCD display
For anyone wondering, the library LiquidCrystal_I2C.h was incompatible with the LCD I used. Finding the repository on the LCD website provided the correct information. The Wire.h library was all that is needed.

Arduino analogWrite() between two pins only working in one direction

I have a set of leds that are setup every other led reversed so when I apply power one way light 1,3,5... light. Change power and 2,4,6... I'm trying to set the brightness using PWM on the digital pins. Here's my code:
unsigned long flashCount = 0;
bool bSwitch = true;
void setup()
{
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop()
{
if((flashCount + 1000) < millis())
{
if(bSwitch)
{
analogWrite(6, 0);
analogWrite(7, 1);
bSwitch = false;
}
else
{
analogWrite(7, 0);
analogWrite(6, 1);
bSwitch = true;
}
flashCount = millis();
}
}
If I change analogWrite to 255 instead of 1, it will switch both sets of leds. If I change analogWrite to 127 or less, only one set will light. If I switch the led wires to the pins, the problem switches to the other set of lights.
The leds are like so:
GPIO pin 6 --------.-LED+.---.-LED+.---.-LED+.---.-LED+.---|
GPIO pin 7 ---.+LED-.---.+LED-.---.+LED-.---.+LED-.--------|
Change the connection of the LEDs to pins that both support PWM.
Not all pins support PWM. The analogWrite documentation specifies which pins depending on which board:
On most Arduino boards (those with the ATmega168 or ATmega328P), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 - 13 and 44 - 46. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.
The other factor is that analogWrite(255) and analogWrite(0) will revert to driving the output as a digital output. So writing 255 causes both pins to output (one as a digital output and the other in PWM mode). But writing 1 to 127 only causes the PWM capable pin to change.
From arduino's manpages:
Syntax
analogWrite(pin, value)
Parameters
pin: the pin to write to. Allowed data types: int.
value: the duty cycle: between 0 (always off) and 255 (always on). Allowed data types: int
Using an analogWrite with a value of 1 is essentially near-zero. 255 would be full voltage. You're attempting to use analogWrite() as if it was digitalWrite().
Consider using digital write instead in your code: https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/
As for your LED's behavior, it seems like your circuit needs to be debugged as well: Your circuit will only allow current to flow when pin 7 is on. Diodes (Light Emitting Diodes) only allow current in one direction. If you're intending to have the LED's alternate, they should all be oriented with the positives pointing toward their GPIO pin and where they meet they should be grounded with a pull-down resistor.

MEGA 2560: Crashes when Adafruit FT6206 ctp function called using code with Interrupt

Arduino MEGA 2560: I want an interrupt to execute when a button push occurs on a rotary encoder. I have the code working. However, if I add a function call to Adafruit's FT6206 library the MEGA 2560 hangs. (Evidence of this is that the Serial.println command text is cut-off mid stream of display.) Why?
Hardware setup: rotary encoder push button has two pins: one side is connected to the interrupt pin with a pull-up for power, and the other side tied to ground.
No other hardware or shield is connected. The hardware set up is only the rotary encoder wired to the MEGA 2560 to demonstrate the issue in its simplest form; however, I have the same issue when the TFT display shield is installed.
The MEGA 2560 is the INLAND brand. The rotary encoder is from Adafruit.
Here is the code:
#include "Adafruit_FT6206.h"
// Global Variables
const byte RotarybuttonPin = 21;
volatile byte Rotarybutton_Pressed_Flag;
TS_Point touchpoint;
Adafruit_FT6206 ctp = Adafruit_FT6206();
void setup() {
ctp.begin(70);
Serial.begin(115200);
pinMode(RotarybuttonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(RotarybuttonPin), Button_Pin_ISR, LOW);
}
void Button_Pin_ISR() {Rotarybutton_Pressed_Flag = 1;}
void loop() {
Serial.println("In Loop!");
touchpoint = WAIT_FOR_SCREEN_TOUCH();
}
TS_Point WAIT_FOR_SCREEN_TOUCH() {
CHECK_ROTARY_ENCODER();
Serial.println("Entering 'While'");
//This next line locks the MEGA 2560 up with ctp function call
while (!ctp.touched()) {CHECK_ROTARY_ENCODER();}
return (touchpoint);
}
void CHECK_ROTARY_ENCODER() {
if (Rotarybutton_Pressed_Flag) {
Serial.println("PRESSED");
Rotarybutton_Pressed_Flag = 0;
}
else {Serial.println("NOT PRESSED");}
delay(190);
}
Discovered the issue and wanted to share the solution with the community.
The MEGA2560 has six interrupt pins: 2, 3, 18, 19, 20, 21. However, when using the FT6205 which is part of Adafruit's 2.8" TFT shield, the interrupt pins 20 and 21 become unavailable as they are being used by the I2C bus. Interrupts are limited to pins 2, 3, 18, and 19.
This limitation is a bit problematic as the TFT shield blocks easy access to pins 2 and 3. However, it is possible to share a header pin with the shield with a #22 gauge wire.
Pins 18 and 19 are not blocked by the TFT shield and could be used. However, I want to use these pins for their interrupts in detecting clockwise and counterclockwise rotary encoder movement.

Arduino UNO with LCD, strange readings from rotary encoder

I have an I2C 16x2 LCD display connected to an Arduino Uno's A4 (SDA) and A5 (SCL) pins. No problem with the display, it works properly.
Then I have a rotary encoder connected to pins D3 (INT1) and D4. The INT1 pin is used as interrupt to read the encoder, and the reading is sent via Serial.print() to the Serial monitor. There are debounce CAPs connected to the rotary encoder. The encoder pins use the Arduino's internal pullups.
The interrupt is attached to read encoderPinA when encoderPinB is falling from HIGH to LOW. When turning the rotary clockwise, encoderPinA is LOW, and when turning it counter-clockwise, encoderPinA is HIGH.
Now, when there is nothing in the main loop, I get ++++++++++ signs on the serial monitor when turning the rotary clockwise, and ---------- signs when turning it counter-clockwise, as I should.
But if I uncomment those two lines that print to the LCD, I start to get erratic readings from the rotary encoder, like this: -++-++-++-+++-++-+++-++--+.
What's going on? Is the LCD interfering with interrupt pins?
#define encoderPinA 4
#define encoderPinB 3
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
attachInterrupt (digitalPinToInterrupt(encoderPinB), readEncoder, FALLING);
}
void loop() {
//lcd.setCursor(0, 0);
//lcd.print("test");
}
void readEncoder() {
if (digitalRead(encoderPinA) == LOW) Serial.print("+");
else Serial.print("-");
}
Sorry folks, this problem was clearly a cable related issue.
I was using the same non-shielded flat cable to carry out rotary and lcd signals, and there was some interference, because when I switch to separate cables, the erratic behaviour is gone.

Arduino UNO analogRead always returns 1023

So my problem is as title says: Arduino UNO analogRead always returns 1023.
But when I burn the same sketch in Arduino Mega 2650 everything works like a charm.
I have tried to change Atmel chips on the UNO, have tried like 3 of them (ATMEGA328P-PU) and nothing changes.
I'm trying to count signals from a hall effect sensor and display the count on a 7 segment display.
Here is the code:
#include "SevSeg.h"
SevSeg sevseg;
volatile int rpmcount;
void setup() {
Serial.begin(9600);
pinMode(2,INPUT_PULLUP);
rpmcount = 0;
sevseg.Begin(1,3,4,5,6,7,8,9,10,11,12,13);
}
int border=15;
void loop() {
int tmp=0;
tmp = analogRead(0);
if(!digitalRead(2))rpmcount=0;
Serial.println(tmp,DEC);
if(tmp<=border && res >border){
rpmcount++;
if(rpmcount>9999)rpmcount=0;
}
res=tmp;
sevseg.NewNum(rpmcount,(byte) 0);
sevseg.PrintOutput();
}
Any help would be much appreciated
This sounds to me as if you had the internal pullup resistor on the ADC pin enabled.
generic checklist:
ACD bit in ACSR is 0 (comparator enable)
MUX bits in ADMUX set properly
correct AREF selected
ADC pin set as input
internal pull up resistors are deselected

Resources