Temperature + Arduino + Number

I am trying to read the temperature of the Temperature Module, and show it in the Number Module through Arduino, but the number displayed in the Number Module is lower than the number displayed if I connect the Number Module directly in the Temperature Module.

My Arduino code is:

const int TemperaturePin = A0; // the pin that the Temperature Sensor is attached to
const int DisplayPin = 5; // the pin that the display is attached to
int temperature = 0; // current state of the temperature

void setup() {
analogRead (TemperaturePin);
}

void loop() {
temperature = analogRead (TemperaturePin);
analogWrite(DisplayPin, temperature);
delay(10000);
}

Any help to solve it?

After I solve it I will keep moving with my project (I want to replicate in littleBits a temperature control device I have done with Arduino)

Analog reading and analog writing are a bit different in Arduino-speak than they are in just littleBits alone.

The analogRead function returns a value from 0 to 1023, where 0 volts will return 0 and 5 volts will return 1023.

And analogWrite will output 0-5 volts in response to a range of 0 to 255 for the write value.

Arduino has a built-in function, map that can change a number from one range (0-1023) to another (0-255).

So try this:

void loop() {
    temperature = analogRead(TemperaturePin);
    temperature = map(temperature, 0, 1023, 0, 255);
    analogWrite(DisplayPin, temperature);
    delay(1000);
} 

Your setup() needs a bit of fixing too:

void setup() {
    pinMode(DisplayPin, OUTPUT);
}

Please note the word “map” above contains a link to the reference page on the arduino.cc website. If you plan on getting into your arduino bit’s guts, you will spend a lot with that reference!

Have fun!

3 Likes

@chris101,
thanks! I tried to use the map function, but with other parameters. I though the temp sensor output would have the same range as they connect together. I will move forward replicating one of my Arduino projects in littleBits and share it when I finish.
My best, Henrique

Most welcome @henrique007!

A note of clarification on what I wrote last night: The temperature bit does not output 0-1023, that is how Arduino ‘interprets’ a 0-5v analog input. Likewise, when it outputs a number 0-255, the bits see that as an analog 0-5volts. So the temp bit and the number bit do have the same range.

The arduino has 4 times the resolution on analog inputs as it does for it’s analog outputs. I am not sure why it was designed that way.

I’m looking forward to seeing what you come up with. I have not used a temperature bit … yet.

Hi @chris101 and other littleBits friends, :smile:

@leo, @angelost, @AldoMunhoz and @georgebrindeiro, I hope you enjoy it.

There is my first working littleBits project. It is a simplified version of a project I have done in Arduino few years ago.

I will keep working in some new versions trying to solve some issues I couldn’t do today and start building some new functions.

A. What I couldn’t make today:
(i) Use a button in the d0 input, keeping it pushed while I change the temperature trigger in the dimmer;
(ii) Make a loud noise in the Speaker if the temperature overcome the trigger;

B. What are the new functions I want to build:
(i) Send a message in my iPhone when if the temperature overcome the trigger;
(ii) Place the temperature sensor in a different place and read it through the web
(iii) Advance in remote communications reading signals in different places and moving things remotely.

Anyone willing to help is welcome!

A picture of my first littleBits and its Arduino code:


/*
littleBits Temperature Control Module
 created 26 Apr 2015 by Henrique Castro

based on: 

(1) littleBits Arduino Module
 created  27 Sep 2005 by Tom Igoe   http://arduino.cc/en/Tutorial/ButtonStateChange
 modified 30 Aug 2011 by Tom Igoe
 modified for littleBits 06 Mar 2014 by Matt Richard
 
(2) Arduino Temperature Control
 created 2013 by Henrique Castro
 
with support from:
(1) Chris Heilman in the littleBits Forum
 

What is supposed to happen:
 * when the top dimmer (d0) is turned counterclockwise: turn on the led if the current temperature is higher than the max temperature
 * when the top dimmer (d0) is turned clockwise: set up the max temperature
 
Circuit:
 * 01 Arduino Module
 * 02 Dimmer Modules
 * 01 Temperature Sensor Module
 * 01 Fork Module
 * 01 Number Module
 * 01 Bargrath Module 

 This sketch includs the following commands:
 pinMode(pin, mode);                  pin = a digital pin, mode = OUTPUT or INPUT
 Serial.begin(baudrate);              baudrate = speed of serial communication
 digitalRead(pin);                    pin = a digital pin
 analogRead(pin);                     pin = a analog pin
 if(conditional){}                    conditional = logical statement that evaluates to TRUE or FALSE
 digitalWrite(pin, state);            pin = a digital pin, state = HIGH or LOW
 delay(time);                         time: milliseconds
 map(data, min1, max1, min2, max2);   convert data range from 1 to 2
 
*/

 // constants are variables whose values won't change:
 byte StateDimmerPin = 0;    // the pin that the StateButton is attached to
 byte TemperaturePin = A0;    // the pin that the Temperature Sensor is attached to
 byte Temperature_maxPin = A1;    // the pin that the Temperature Dimmer is attached to
 byte DisplayPin = 5;         // the pin that the display is attached to
 byte LedPin = 9;              //

 // these variables values will change:
 int dimmerState = 0;              // current state of the dimmer used to turn on the temperature trigger
 int temperature = 0;              // current state of the temperature
 int temperature_max = 255;        // trigger of max temperature
 int led = 0;
 
 void setup() {
 
    pinMode(StateDimmerPin, INPUT);
    pinMode(DisplayPin, OUTPUT);
    pinMode(LedPin, OUTPUT);
    analogRead(TemperaturePin);
    analogRead(Temperature_maxPin);
    // initialize serial communication:
    Serial.begin(9600);
 }

 void loop() {
   
   // read the temperature input pin:
   temperature = analogRead(TemperaturePin);
   temperature = map(temperature, 0, 1023, 0, 255);
   
   // read the pushbutton input pin:
   dimmerState = digitalRead(StateDimmerPin);
   
   // compare the buttonState to its previous state
   if (dimmerState == HIGH) {
     // dimmer is turned right
       temperature_max = analogRead(Temperature_maxPin);
       temperature_max = map(temperature_max, 0, 1023, 0, 255);
       analogWrite(DisplayPin, temperature_max);
       delay(1000);

       Serial.println(dimmerState);
       Serial.println("temperature trigger on");
       Serial.print("temperature:  ");
       Serial.println(temperature);
       Serial.print("max temperature:  ");
       Serial.println(temperature_max);
     } 
     else {
       // dimmer is turned left
       analogWrite(DisplayPin, temperature);
       delay(1000);
       
       if (temperature > temperature_max) {
         led = 255;
         analogWrite(LedPin, led);
         Serial.println("*** A L A R M ***");
       }
       else {
         led = 0;
         analogWrite(LedPin, led);
       }  
       Serial.println(dimmerState);
       Serial.println("temperature trigger off");
       Serial.print("temperature:  ");
       Serial.println(temperature);
       Serial.print("max temperature:  ");
       Serial.println(temperature_max);       
     }  
 }
 
4 Likes

It seems right, not is working?

It is working, but I will upgrade it with new functions!

I see. Using Arduino is very easy.

Can I get some help [sketch] to use Arduino Module, Temperature Sensor Module, Cloudbit Module and post the temperature on to https://data.sparkfun.com/input/o8oM9qza04H192661KKq?private_key=yz16pNlxgeFAqZvvA22y&temp=?

I am playing with your code and the one thing I don’t understand is how to convert the range 0 - 255 to the actual temp. Can you help?

Never mind. I’m a dork as soon as I wrote this, I got the answer. Multiply the 0 - 255 numbers by 0.38823529411 to get the actual F. Temp.