I have an ATtiny hooked up to two line scan modules. I'm sending digital signals to the line scan modules then receiving the analog output and storing it in an array. I have an if statement that will make the LED on the ATtiny programmer blink when the number are below a certain range (meaning that lens is covered).

Here is my code:

#include <SoftwareSerial.h>
const int rx=0;
const int tx=1;

SoftwareSerial mySerial(rx,tx);
int CLK = 2;
int SI = 1;
int Aout = A3; 
int Aout1 = A2;

int LED = 0;

int pixelsArray[128];   //Array to hold the values of the individual pixles.
int pixelsArray1[128];

 void outputPixels()
  for(int j = 0; j < 128; j++)
    digitalWrite(LED, LOW);

    if(pixelsArray[j] < 100)
      digitalWrite(LED, HIGH);
      digitalWrite(LED, LOW);

The problem that I am having is that when I test my code with one line scan module it works just fine. You will notice that in the readPixels() I have the statement pixelsArray1[i]=analogRead(Aout1); commented out. However, when I try to use it with two the LED start blinking regardless of how much light is hitting the line scanner.

The strange thing is that EVEN when I comment out the digitalWrite(LED, HIGH); statement in the outputPixels() method...THE LED STILL BLINKS!!!

Since the ATtiny does not support URT and can't use the console in Arduino I have no idea how to debug this. Does anyone here have any clue what is wrong?

Here is the datasheets

ATtiny: http://www.atmel.com/Images/Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet.pdf

ATtiny Programmer: https://learn.sparkfun.com/tutorials/tiny-avr-programmer-hookup-guide/?_ga=1.59946280.467360091.1464906372

  • The ATtiny2313 has only 128 bytes of RAM. Your two pixel arrays, together, need 512 bytes. No way this can work. Commented Jun 8, 2016 at 15:45
  • Sorry I linked the wrong datasheet. I am using the ATtiny85 which has 512 bytes of RAM. Commented Jun 8, 2016 at 15:52
  • 2
    That's still not enough. Your arrays are eating all the RAM of the ATtiny85, thus corrupting the stack. Commented Jun 8, 2016 at 15:53
  • If I am reading the ATtiny datasheet correctly then I have a total of 512 bytes of RAM. Meaning that u should have enough space for two 128 arrays. What am I missing? Sorry if I'm being stupid, but I'm really outside of my field right now. Commented Jun 8, 2016 at 16:25
  • Your program needs RAM for storing all its globals (in the “data” and “bss” sections of the RAM), and the return addresses for the functions you call (“stack” section). You cannot allocate all the RAM to your arrays and expect it to work. Commented Jun 8, 2016 at 16:47

2 Answers 2


Since you do not have enough RAM for your arrays, something has to give. A few options:

  1. Do you really need the 10-bit precision of the ADC? If you can live with 8 bits, then use 8-bit arrays (type uint8_t) and store only the 8 most significant bits of each reading (pixelsArray[i]=analogRead(Aout)>>2). This will make the arrays half as big.
  2. If you really need 10-bit precision, maybe you do not need 128-pixel resolution? Then you could store only every other pixel. This will also halve the array sizes.
  3. If you do need both the full precision and the full resolution, then you will have to pack the bits tight. You can pack up to 4 readings in 5 bytes. But this will increase the complexity of your program.

Addendum: To declare the arrays as being made of 8-bit integers, just replace

int pixelsArray[128];
int pixelsArray1[128];


uint8_t pixelsArray[128];
uint8_t pixelsArray1[128];

Here “uint8_t” means “unsigned 8-bit integer”, whereas “int” is a regular integer, i.e. signed and, on the AVRs, 16-bits long.

  • These are pixels so I don't need a lot of precision, but how can I use 8-bit arrays? Do I just add the >> like you said or do I need to change it in the declaration? Also doesn't each integer take up 2 bytes? Commented Jun 8, 2016 at 17:06
  • @FortunaIwasaki: You have to change the declaration also. See the amended answer. Commented Jun 8, 2016 at 17:16
  • Thank you so much for your help, but could you also explain what you meant by "pack the bits tight"? How can you keep the same number of pixels and still store them as integers, yet use less space? Commented Jun 8, 2016 at 18:20
  • @FortunaIwasaki: By using bitwise operations, you can manage the storage at the bit- (instead of byte-) level. Then, you would allocate 10 bits per pixel, which amounts to 2560 bits (320 bytes) for the 256 pixels. Commented Jun 8, 2016 at 19:25

According to the Specs of the Attiny, this microcontroller only has 128 bytes of memory (RAM).

In the code you declare two arrays:

int pixelsArray[128];   //Array to hold the values of the individual pixles.
int pixelsArray1[128];

which will occuppy much more memory than the available on this microcontroller. Try to reduce the size of the array. Each int entry occupies 2 bytes according to the link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.