Paul Sutton

Arduino

Code Club Electronics 18

I was intending to use a potentiometer to control the motor speed, however using an arduino seems rather overkill for this. So I will just do that directly.

I decided to build a car instead, so far this has 2 wheels and just goes forward.

#include <AFMotor.h>

AF_DCMotor motorA(3);
AF_DCMotor motorB(4);
/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-potentiometer
 */



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

// the loop routine runs over and over again forever:
void loop() {

  
  
//Motor spinning clockwise
  motorA.run(FORWARD);
  //Speed up
  motorA.setSpeed(100);  
  motorB.run(FORWARD);
  //Speed up
  motorB.setSpeed(100); 
    


  }
  

#Arduino,#Robot,#Car,#InitialCode


MastodonPeertubeQoto sign up

#LookingForWork

Donate using Liberapay

Code Club Electronics 17

So, further to the previous post earlier. I made a quick video showing the motor and code in action. Paper is on just to make it easier to see the motor moving.

Video

Tags

#DCMotor,#Arduino,#Demonstration


MastodonPeertubeQoto sign up

#LookingForWork

Donate using Liberapay

Code Club Electronics 16

Just been looking at the Arduino Motor shield. First off I found some example code along with the all important motor shield library which can be found here

The library is compressed, so once downloaded, I used the Arduino IDE tool to prepare the library software so it can be used.

The video below just shows how to do this. You still need to include the library in your sketch code with:-

#include <AFMotor.h>

Video

Tags

#DCMotor,#Arduino


MastodonPeertubeQoto sign up

#LookingForWork

Donate using Liberapay

Code Club Electronics 14

I want to build something for the science kit to make it easier to stir liquids. I have found a project for a DC motor controlled by a potentiometer. that also uses an Arduino microcontroller. This should be easy enough to build and attach a stirrer to. This can be soldered up, battery attached, power switch may also be needed.

Should then be easier to stir liquids for science experiments.

Tags

#Arduino,#Electronics,#Science,#Chemical,#Stirrer,#DC,#Motor, #Control


MastodonPeertubeQoto sign up

#LookingForWork

Donate using Liberapay

Code Club Electronics 13

So further to my previous attempt, I have now made some progress with this. New code is as follows

// 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, (voltage));   // 1khz tone to buzzer
  delay(analogValue);                       // wait for time period linked to pot input value
  tone(buzzer, (voltage));    // 1khz tone to buzzer

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

The main difference here is that I am binding the buzzer tone to the voltage

  float voltage = floatMap(analogValue, 0, 1023, 0, 5);
  tone(buzzer, (voltage));   // 1khz tone to buzzer
  delay(analogValue);                       // wait for time period linked to pot input value
  tone(buzzer, (voltage));    // 1khz tone to buzzer

This sort of works, but the frequency isn't very high.

Tags

#Arduino,#Electronics


MastodonPeertubeQoto sign up

#LookingForWork

Donate using Liberapay

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

#LookingForWork

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

#LookingForWork

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

#LookingForWork

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

#LookingForWork

Donate using Liberapay

Code Club Electronics 8

So following on from the previous post. I had to look this up to remind myself how to specific a pin. Not used Arduino in a while. I found the following code as part of a stackoverflow post

However, in this, the two LEDs flash on and off alternately. Delay is set by a single line.

int delayPeriod = 1000;

So this is more so I can learn how to specify a pin.

int ledPin = 12;
int ledPin2 = 13;
int delayPeriod = 1000;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}

void loop()
{
  digitalWrite(ledPin, HIGH);
  digitalWrite(ledPin2,LOW);
  delay(delayPeriod);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin,LOW);
  delay(delayPeriod);
  //delayPeriod = delayPeriod - 10;
}

In the video, I am just changing a single line of code to cause the delay.

Timings are in milliseconds or seconds

Links

Tags

#Electronics,#Code,#Arduino,#Hacking.#LED,#Blink


MastodonPeertubeQoto sign up

#LookingForWork

Donate using Liberapay