title: Temperature conversion in Python: How to convert int (0 - 255) which is a byte object to degrees Celsius in Python
body: <p>We are currently working on an Arduino Uno project and getting stuck at the conversion of the integer data to degrees celcius. This code is working however it converts the binary packed data (\xd01) etc. to int (0-255). Our question is: how to convert the integer value to read out a certain degree of Celcius. For example: int 2 = 2 degrees celcius and 255 = 35 degrees Celcius</p>
  
  <p>This is our Python code with the Pyserial module <br>
  <pre><code>import serial
  import struct
  ser = serial.Serial('COM3', 19200, timeout=5)
  while True:
      tempdata = ser.read(2)
      x= struct.unpack('!BB', tempdata)
      print(x)
  </pre></code></p>
  
  <p>And this is the code of the temperature conversion on our Arduino Uno, it is written in C.</p>
  
  <p><pre><code>
      #define F_CPU 16E6</p>
  
  // output on USB = PD1 = board pin 1
  // datasheet p.190; F_OSC = 16 MHz &amp; baud rate = 19.200
  #define UBBRVAL 51
  
  void uart_init()
  {
      // set the baud rate
      UBRR0H = 0;
      UBRR0L = UBBRVAL;
      // disable U2X mode
      UCSR0A = 0;
      // enable transmitter
      UCSR0B = _BV(TXEN0);
      // set frame format : asynchronous, 8 data bits, 1 stop bit, no parity
      UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
  }
  void transmit(uint8_t data)
  {
      // wait for an empty transmit buffer
      // UDRE is set when the transmit buffer is empty
      loop_until_bit_is_set(UCSR0A, UDRE0);
      // send the data
      UDR0 = data;
  }
  
  void init_adc()
  {
      // ref=Vcc, left adjust the result (8 bit resolution),
      // select channel 0 (PC0 = input)
      ADMUX = (1&lt;&lt;REFS0);
      // enable the ADC &amp; prescale = 128
      ADCSRA = (1&lt;&lt;ADEN)|(1&lt;&lt;ADPS2)|(1&lt;&lt;ADPS1)|(1&lt;&lt;ADPS0);
  }
  uint8_t get_adc_value()
  {
  
      //ADMUX |= 1
      ADCSRA |= (1&lt;&lt;ADSC); // start conversion
      loop_until_bit_is_clear(ADCSRA, ADSC);
      return ADC; // 8-bit resolution, left adjusted
  }
  
  /*
  ((value / 1024 * 5) - 0. 5) * 100
  */
  
  int main(void) {
      init_adc();
      uart_init();
      //int x;
      while(1)
      {
  
          int x = get_adc_value();
          int temp = ((((float) x / 1024) * 5) - 0.5) * 100;
          transmit(temp);
          _delay_ms(200);
      }
  }
  </code></pre>