Paul Sutton

TempSensor

Code Club Electronics 12

So the next project is to try and link the potentiometer value to a buzzer tone.

So, using some tutorial code for the buzzer here. I have tried to modify to change the tone of the buzzer depending on the value from the potentiometer.

// the setup function runs once when you press reset or power the board

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
const int buzzer = 5;


void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(buzzer, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  // read the input on analog pin A0:
  int analogValue = analogRead(A0);
  // Rescale to potentiometer's voltage (from 0V to 5V):
  float voltage = floatMap(analogValue, 0, 1023, 0, 5);
  tone(buzzer, 1000);   // 1khz tone to buzzer
  delay(analogValue);                       // wait for time period linked to pot input value
  tone(buzzer, 1000);    // 1khz tone to buzzer

  //https://www.instructables.com/How-to-use-a-Buzzer-Arduino-Tutorial/
}

What I have now is a little buggy, If I bind the actual pot value between 0 and 1023, anything below 20hz is inaudible, so that the frequency is within the human hearing range (20hz – 20khz)

Not quite sure what is going to work, sharing this as work in progress.

Links

Tags

#Electronics,#Code,#Arduino,#Hacking.#TempSensor


MastodonPeertubeQoto sign up

Donate using Liberapay

Code Club Electronics 11

I am now combining a LED blink routine, with reading the potentiometer value input.

// the setup function runs once when you press reset or power the board

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}


void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}


  

// the loop function runs over and over again forever
void loop() {
  // read the input on analog pin A0:
  int analogValue = analogRead(A0);
  // Rescale to potentiometer's voltage (from 0V to 5V):
  float voltage = floatMap(analogValue, 0, 1023, 0, 5);
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(analogValue);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(analogValue);                       // wait for a second
}

Video

Video illustrates how rotation of the potentiometer is reflected in the LED blink delay.

Links

Tags

#Electronics,#Code,#Arduino,#Hacking.#TempSensor


MastodonPeertubeQoto sign up

Donate using Liberapay

Code Club Electronics 10

So following on from the previous post. Still using the Easy module shield) I have found out how to read the analogue input from the potentiometer. So far this gives a value and voltage.

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-potentiometer
 */

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin A0:
  int analogValue = analogRead(A0);
  // Rescale to potentiometer's voltage (from 0V to 5V):
  float voltage = floatMap(analogValue, 0, 1023, 0, 5);

  // print out the value you read:
  Serial.print("Analog: ");
  Serial.print(analogValue);
  Serial.print(", Voltage: ");
  Serial.println(voltage);
  delay(1000);
}

So the two lines (the first is a comment) sets the program to read from A0

 // read the input on analog pin A0:
  int analogValue = analogRead(A0);

With output looking like

 // read the input on analog pin A0:
  int analogValue = analogRead(A0);

READING THE LIGHT SENSOR

By changing this to

 // read the input on analog pin A1:
  int analogValue = analogRead(A1);

It is possible to read the light sensor on the same board.

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-potentiometer
 */

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin A0:
  int analogValue = analogRead(A0);
  // Rescale to potentiometer's voltage (from 0V to 5V):
  float light = floatMap(analogValue, 0, 1023, 0, 5);

  // print out the value you read:
  Serial.print("Analog: ");
  Serial.print(analogValue);
  Serial.print(", Light: ");
  Serial.println(light);
  delay(1000);
}

I have edited the lines above so that it reflects the fact we are now reading the light sensor.

  float light = floatMap(analogValue, 0, 1023, 0, 5);

  // print out the value you read:
  Serial.print("Analog: ");
  Serial.print(analogValue);
  Serial.print(", Light: ");
  Serial.println(light);
  delay(1000);
}

You will need to change the other lines to suit, as some of the comments still refer to the potentiometer.

Video

Video illustrates how rotation of the potentiometer is reflected in the output values.

Links

Tags

#Electronics,#Code,#Arduino,#Hacking.#TempSensor


MastodonPeertubeQoto sign up

Donate using Liberapay

Code Club Electronics 9

So following on from the previous post. I had to look this up to remind myself how to specific a pin.

So my next project, was to try and get the DHT11 temperature and humidity module working. I had a problem with the library for this, so looked at the code, used the section of this that used the LM35 temperature module (which can also be found on the Easy module shield)

float tempC;
int tempPin = 2;
byte temperature = 0;

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{

  tempC = analogRead(tempPin);           //read the value from the sensor
  tempC = (5.0 * tempC * 100.0)/1024.0;  //convert the analog data to temperature

  Serial.print("LM35 - Temperature: "); 
  Serial.print((byte)tempC);
  Serial.println(" Celcius");
  delay(500); // delay 5 seconds
}

So just switched to pin 2 above

int tempPin = 2;

And modified the code to give nicer output. The Arduno IDE allows for both plotting of data on a graph and also just using the serial monitor.

Output looks like this

LM35 - Temperature: 18 *C
LM35 - Temperature: 18 *C
LM35 - Temperature: 18 *C

Links

Tags

#Electronics,#Code,#Arduino,#Hacking.#TempSensor


MastodonPeertubeQoto sign up

Donate using Liberapay