After trying a couple of different sensors / input devices that didn't work with the ATTiny I settled on using a light sensor.
I wrote a program that reads the light sensor and then turns on the LED if it reaches a specific threshold.
I needed to use a INPUT_PULLUP to activate the internal resistor of the microcontroller.
Below is the code I ended up using.
lightsensor.ino #define PhC_PIN PB3 //Define the Photoconductive Cell Pin #define LED_PIN PB2 //Define the LED Pin int phcellReading; //Variable for PhCell Reading #include <SoftwareSerial.h> //Including the SoftwareSerial library #define RX PB0 //Defining the MOSI Pin as RX #define TX PB1 //Defining the MISO Pin as TX SoftwareSerial mySerial(RX, TX); //Defining the SoftwareSerial Pins (RX, TX) void setup() { mySerial.begin(2400); //Opening the serial, 2400 as data rate pinMode(PhC_PIN, INPUT_PULLUP); //Setup the internal Pullup pinMode(LED_PIN, OUTPUT); } void loop(){ phcellReading = analogRead(PhC_PIN); //Read the PhCell mySerial.print("Analog reading = "); mySerial.println(phcellReading); //Print PhCell if(phcellReading > 120){ //If low brightness digitalWrite(LED_PIN, HIGH); //Light up LED }else{ digitalWrite(LED_PIN, LOW); //turn off LED } delay(100); //wait a bit }
I soldered the light sensor on two wires so I can easily hook it up to my board.
One end needs to be connected to GND and the other to the pin specified in the code. Here I used the PB3/ADC3
After uploading the code using the Arduino (like last week) you can hook up to the FTDI Cable (also like last week).
It looks something like this then:
After connecting the FTDI Cable to my computer and using the serial monitor I can now hold my finger over the light sensor to make it read darkness.
If the light sensor reads darkness it will turn ON the LED.
Serial Monitor shows the analog input of the light sensor.
I first tried to use a SD card reader, since I wanted to use one in my final project.
I've written this program:
sdCard.ino //Including the librarys #include <SoftwareSerial.h> #include <SD.h> #include <SPI.h> //Setting up the SoftwareSerial (RX, TX) //RX turned of because not enough pins SoftwareSerial mySerial(0, PB1); //Set Chip Select to pin #define CS_PIN PB4 //File Object File myFile; void setup() { //File we will read char myFileName[] = "MyFile.txt"; //Holder of File content String LineString = ""; //Opening the SoftwareSerial, 2400 as data rate for ATTiny mySerial.begin(2400); //Wait for SoftwareSerial to open. while (!mySerial) {} //Starting SD Card Setup mySerial.println("Setting up SD card..."); mySerial.println(); //Setting CS Pin as output pinMode(CS_PIN, OUTPUT); //Check if SD is found if(!SD.begin(CS_PIN)){ mySerial.println("SD did not set up..."); while(1); } mySerial.println("SD set up!"); mySerial.println(); //Reading the file mySerial.println("Reading MyFile.txt..."); mySerial.println(); //Open our File myFile = SD.open(myFileName, FILE_READ); //Keep reading while(myFile.available() != 0){ LineString = myFile.readStringUntil('\n'); mySerial.println(LineString); } myFile.close(); mySerial.println(); mySerial.println("Done"); } void loop() { }
Sadly, I couldn't upload this program to my microcontroller, because the microcontroller seems to be incompatible with the SD.h and / or SPI.h
Next, I tried a humidity sensor:
humidity.ino #include <DHT.h> #include <DHT_U.h> //Including the librarys #include <SoftwareSerial.h> //Setting up the SoftwareSerial (RX, TX) //RX turned of because not enough pins SoftwareSerial mySerial(0, PB1); #define DHTPIN PB3 #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHTTYPE); void setup() { //Opening the SoftwareSerial, 2400 as data rate for ATTiny mySerial.begin(2400); //Wait for SoftwareSerial to open. mySerial.println("DHT:"); dht.begin(); } void loop() { // Wait a few seconds between measurements. delay(2000); // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { mySerial.println("Failed to read from DHT sensor!"); return; } // Compute heat index in Fahrenheit (the default) float hif = dht.computeHeatIndex(f, h); // Compute heat index in Celsius (isFahreheit = false) float hic = dht.computeHeatIndex(t, h, false); mySerial.print("Humidity: "); mySerial.print(h); mySerial.print(" %\t"); mySerial.print("Temperature: "); mySerial.print(t); mySerial.print(" *C "); mySerial.print(f); mySerial.print(" *F\t"); mySerial.print("Heat index: "); mySerial.print(hic); mySerial.print(" *C "); mySerial.print(hif); mySerial.println(" *F"); }
But I got the same compilation error because of incompatible libraries. Thats when I decided to use the light sensor since it doesn't require libraries.