Trinket and AnalogRead issues (SOLVED)

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
AndyFun
 
Posts: 17
Joined: Fri Aug 08, 2014 5:49 pm

Trinket and AnalogRead issues (SOLVED)

Post by AndyFun »

EDIT: (SOLVED!!) analogRead was working the whole time. The real issue: I was running out of SRAM, because the ".toCharArray()" function was too large to complete in the Trinket's memory. It was just cleverly disguised as a read issue. (shakes fist) I instead used "itoa()" to convert the the int from the analogRead(1), into a char to be sent by the TX. This also droped my program size from above 4k to well under 3k. I recommend reading the whole thread to understand what's going on. Thanks for the help Bill!

Hey all,
I've been fighting with this for a few days now, and I cant for the life of me get an analogRead(1) off of PB2 (D2). I've tried the other analog pins also with no success.

I'm using A1 (D2) to get a voltage reading off of a divider circuit coming from a 9V battery. The code works on an UNO just fine, but I can't seem to get anything off of the trinket. I've replaced every component in the prototype one by one to see if it's a circuit error, but I get the same results with everything new, including bread board.

Could I have a defective unit? Can the analog function be broken, but everything else works fine? (like PWM and digital read/writes?) Is there a simple way to test this? Maybe an analogWrite to an LED, or a Read from a POT?

Resistors:
R1 = 8.2k ohm
R2 = 1k ohm

sampling from between the two resistors into PB2 (D2) or (A1 when called in code)


//Sample Code
int sensorPin = 1;
String sensorReading;

void setup() {
//empty. If I understand it correctly I only have to set pinMode on digital pins to turn on pull-up
}

void loop(){
sensorReading = String(analogRead(sensorPin)); //Get sensorReading as a String off A1 (so I can convert it to a char* to send over a VW or RH compatible transmitter

// here I would copy it into the buffer as a char array and send it. This part of my code works fine.

}

//End Code

Thanks for any help!
Andy
Last edited by AndyFun on Sun Aug 10, 2014 11:30 pm, edited 6 times in total.

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: Trinket and AnalogRead issues

Post by adafruit_support_bill »

but I get the same results
What are those results?

Post a photo showing your circuit.

User avatar
AndyFun
 
Posts: 17
Joined: Fri Aug 08, 2014 5:49 pm

Re: Trinket and AnalogRead issues

Post by AndyFun »

Thanks Bill,

Here are the pics you requested.

Description:
Power: BAT+ is plugged into VCC of the TX, and then into R1 of the divider (I needed 9.6V unregulated going to both, into the TX for added range, and into the divider for an accurate under load voltage reading) GND is the rail on the far right.

The divider circuit is sampled in between R1 and R2, into PB2 (A1)

#0 is plugged into the ATAD (data) on the TX.

The "results I'm seeing are... nothing... but it's a consistent nothing. When I remove the analogRead from the code, and just have the Trinket send a const char, everything works fine. The analog read doesn't return anything, not even a size of the string it's trying to read.

Should I try a software serial to see if its possibly something else(to get actual Serial.prints?

Should I try to resolder the pins? It shouldn't matter which side of PCB I soldered them on to, right?

Should I reflash the boot loader? Maybe one of the fuses is damaged?

Thanks for the help,
Attachments
WP_20140809_003.jpg
WP_20140809_003.jpg (447.35 KiB) Viewed 274 times
WP_20140809_002.jpg
WP_20140809_002.jpg (478.58 KiB) Viewed 278 times
WP_20140809_001.jpg
WP_20140809_001.jpg (477.25 KiB) Viewed 278 times

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: Trinket and AnalogRead issues

Post by adafruit_support_bill »

The "results I'm seeing are... nothing... but it's a consistent nothing.
analogRead() returns an integer value. Right or wrong, there is no way that it cannot.. If nothing is printing out or being sent, there is a bug somewhere else in your code.

User avatar
AndyFun
 
Posts: 17
Joined: Fri Aug 08, 2014 5:49 pm

Re: Trinket and AnalogRead issues

Post by AndyFun »

Thanks Bill. Below is the code that follows the analogRread. I did some testing and it appears that you are correct. Something is happening in the code after the read. The char array is not getting constructed. Curiously, this works fine on an UNO.

Does the Trinket not like constructors? Or did I do something wrong?

like: (the following code is NOT functional on the trinket)
const int sensorPin = 1;
char strTrans[4];
String voltageOutPut = String(analogRead(sensorPin)); //<-- (edit) the issue1
int strLength = voltageOutPut.length();
voltageOutPut.toCharArray(strTrans, (strLength +1)); //<-- (edit) the issue2, and the real reason it doesnt work
vw_send((uint8_t *)strTrans, strlen(strTrans));
vw_wait_tx();


This code works on an UNO, so maybe the Trinket is different? This is pretty much my last shot. The trinket isnt needed for the prototype, but would just be a "bonus" in terms of size, and power consumption.

Thanks for all the help,
Last edited by AndyFun on Sun Aug 10, 2014 10:10 pm, edited 6 times in total.

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: Trinket and AnalogRead issues

Post by adafruit_support_bill »

The trinket has extremely limited SRAM memory and string operations tend to require a lot of SRAM. You may have better luck working with fixed character arrays.

User avatar
AndyFun
 
Posts: 17
Joined: Fri Aug 08, 2014 5:49 pm

Re: Trinket and AnalogRead issues

Post by AndyFun »

Thanks Bill. I understand the Trinket so much better now. I'll try converting the analogRead into a single char. Since I calculate the value should never be greater than 212, I can just convert that value into an ASCII character and just send that one, instead of a string. And decode it on the receiver's end.

I'll post an update when I have the new version of code operational.

Thanks so much for the help. I feel like a veil of mystery has been lifted.

Cheers,

EDIT: after using itoa() on the transmitter side, and atoi() on the receiver side, the size of the value in bytes didnt matter. Between 1023 and 0 the values were sent correctly. This is benefitial when using analogReference(INTERNAL) to make voltage readings through a 8.2k ohm and 1k ohm divider circuit on a 9V source more precise(ish).

Locked
Please be positive and constructive with your questions and comments.

Return to “Trinket ATTiny, Trinket M0”