Friday, August 28, 2020

Arduino Onboard Status LEDs

Arduino has 4 Onboard LEDs.  Three have pins associated with them, which can be useful.
Below are tutorials to understand and use these onboard LEDs and associated pins and features.
  1. Power LED
  2. Status LED on pin 13
  3. RX status Serial LED on pin 0
    • Receiving data
  4. TX status Serial LED on pin 1
    • Transmitting data  

Arduino Onboard embedded LEDs

Overview


1. Onboard Power green LED

This green  LED is hardwired to be on if the Arduino has power, so there is no software control.


2. Status LED on pin 13

LED_BUILTIN is set to 13

How to blink LED 13


3./4. RX/TX LEDs

Short version: 

Serial input here is a kind of CLI (command line interface) using
  • Serial.begin(<baudRate>)
  • Serial.println(...) for debug  
  • Serial.write(...) for input

Arduino Tutorial #4: Serial Communication 

Understanding Serial use and when TX/RX LEDs light-up

Summary :

  • regular setup,
    • Serial.begin(9600);  // in: baud rate
  • serial input/output 
    • x = Serial.read(...); // in: what writing like '1' or "Hello" or variable like x 
      • reads in
    • Serial.print(...); //  x, the value of variable  or '1' or 
      • used for a lot for debugging
  • what LEDs light up on the Arduino itself. 
    • rx/tx LEDs behind the USB port
Also see Arduino Reference for Serial class: here

Raspberry Pi - Arduino Serial Communication

  • Communicates over USB cable
  • Be use to check the GPIO pins match the Raspberry PI you are using.

More advanced ideas for using Arduino serial communication

Here are ideas on how to explore more advanced ways to serial communication for your projects:

Search  "Communicating between 2 Arduinos" from arduino.cc/reference

  • see Project Hub results

Google "Communicating between 2 Arduinos"

Software communication to Arduino not using Arduino C++:


Saturday, August 22, 2020

What's that buzz from my Ardiuno

Its not good. Something is or may soon be damaged!
I unplugged my Arduino fairly fast and best I can tell, there has been no permanent damage.

I'm still learning causes, but most causes seem to be related to power usage.
In my case I was running 2 DC motors off the Arduino, where 1 motor especially sometimes pulled a lot of current.
The circuit needed to be using an external power source, like a 9V battery to run the motors.

Why would my program run fine sometimes and buzz other times?  In part because I often used only 1 motor at a time and the motor drew less current after a second or so. So my delay time made a difference.

Using a multi-meter and battery, I measured the current each motor drew and found:

1. Not bad idea to know what current is being used, especially by small motors.
2. Two motors need an external power source because exceed the max current the Arduino can support(~2A).

Wednesday, August 12, 2020

Buttons, Buttons, who got the Button connection?

Here 4 basic button connections are documented in breadboard diagrams, so easy to duplicate. The connections come from RS tutorial video, where they are explained in more detail and shown working.

Arduino Video Tutorial - 01 Get to know your Tools with Massimo Banzi (starting at ~4:40)

The Button: 
Buttons, LEDs, jumper cables are available online at Amazon or in person at EPO Houston.

Button Circuit Connections

  • Top/bottom have 2 wire connectors 
    • place on different lines on breadboard
  • Left/right have no connectors
    • line up with breadboard line.
  • Push button to close circuit from left side to right side of button

Breadboard Wiring Diagrams (for tutorial examples)

  1. Push button to light LED directly
  2. Push button with wire to connect button to the LED
  3. Two push buttons in series, so both buttons must be pushed to light LED
  4. Two push buttons in parallel, so if either button (or both) are pushed the LED will light.

Tuesday, August 4, 2020

Robot Wheels -testing 2 DC motors

Robot cars or tanks have 2 motors one for each side. The most basic test is are the wheels connected correctly to motors.

Parts

Ardiuno ,L9110S H Bridge
breadboard, Jumper wires, 9V battery

Wiring

Wired so via signals from the Arduino both polarities, positive and negative, can be used.

From L9110S to Ardiuno (use male to female jumper cables)
(see photo)
  • B1B to Ardiuno pin 9 (red)
  • B1A to Ardiuno pin 3 (blue)
  • GND to Ardiuno GND pin (black)
  • VCC signal to Ardiuno GND pin (red)
  • A1B to Ardiuno pin 11 (gray)
  • A1A to Ardiuno pin 10 (white)

Basic Tests

  • Does each wheel move forward and backwards?
  • Can both wheels move at the same time?
    • change + and - for wheel going in wrong direction
  • Do both wheels move in the same direction?
  • Can the speed be varied?
  • Can change from forward to backwards?
  • Can wheels move at different speeds?

Code

Download code from github traisen/Ardiuno: here
  • Click on Code to download.
  • Unzip
  • Copy both folders into your Ardiuno folder where your other sketch code is 
    • optional, but recommended so listed under Sketchbook in your Ardiuno IDE
  • Open folder and click on file to open
Code Design Overview

variable set-up
  • pin definitions using variable that match the pin names for L9110S and on Arduino
  • pins 3,9,10,11 were chosen because PWM pins which can write analog signals.
setup()
  • define output pins
loop()
  • performs motor tests
local functions:
  • void move_motorA(int d) 
  • void move_motorB(int d) 

TestFor2DCmotorsUsingDigits tests:
  1.   Motor A fwd, bck, off   wait 5 sec.s
  2.   Motor B fwd, bck, off   wait 5 sec.s
  3.   Both Motors fwd, bck, off   wait 5 sec.s
TestFor2DCmotorsUsingAnalog tests:
  • Motor A fwd fast, fwd slow, bck fast, bck slow, off  
  • Motor B fwd fast, fwd slow, bck fast, bck slow, off   
  • Both Motors fwd-fast, bck-fast,   Tests forward, wait (10 secs)
  • Motor A slow motor B fast; wait, 
  • Motor A fast motor B slow; wait, 
  • Both motors: fwd-slow, bck-slow,off, wait

Monday, August 3, 2020

Time out for Best Coding Practices

Of course you want working code, but  make your code readable so:
  1. Can tell if code doing what you want now.
  2. Understand your code in a year.

Best Coding Practices

  • Use variable names that are meaningful. 
    • Abbreviations are ok.
    • Makes code understandable with minimum of comments
  • Avoid hard-coding values in code
    • By using setting a constant variable 
      • use const before declaring the variable
        • const int photoresistorPin = 3; //blue
        • const int tooClose = 40; // 40 cm is too close
        • const int launchCata = 180; // degrees to move to launch catapult
      • In code: if (distance < tooClose) cata.write(launchCata);
    • Then you can change the value in 1 place
      • Move sensor to a different pin on Arduino
      • Maybe better if 20 or 60 cm is too close
      • Maybe 90 degrees is enough to launch catapult
    • Declare variable at top of code, so easy to find
    • Group related variables together
  • Use functions rather than repeating (similar) code via cut/paste
    • shortens code so easier to understand and type in
    • give function name so easy to know what it does
      • int sonicBat() - returns distance measures from ultrasonic sensor
      • void moveMotor(int speed) - move motor at speed requested
    • always put helper functions in same place in code.
      • in blog helper functions come before loop function that uses these functions
  • Comment code to include background info 
    • distance = (duration/2)/29.1; 
    • // speed of sound is: ~29.1 microsecs/cm or 1/29.1 cm/microsec
    • For Arduino the wiring info in words and color of jumper wire using can help
      • // L9110S pins 
      • const int B1B = 9; // red  to pin 9
      • const int B1A = 3; // blue to pin 3
      • // GND             // purple to GND  by 5V on Arduino
      • // VCC signal      // red    to 5V  by GND on Arduino 
      • const int A1B = 11; // gray  to pin 11
      • const int A1A = 10; // white to pin 10

Servo Catapult with ultrasonic distance sensor

A Popsicle stick and rubber band catapult waits for something to get close and launches a cotton ball at it. 
  • An ultrasonic sensor is used to test distance to nearest object. 
  • A servo motor releases the catapult.

Wiring

Servo motor connections:
  • brown to Neg on 9V battery
  • red to Pos on 9Vbattery
  • yellow to signal output pin on Arduino
    • use analog pin or PWM pin marked with ~
Ultrasonic distance sensor connections;
  • Gnd to Gnd on Ardiuno [black]
  • Trigger to Arduino (using pin ) [green]
  • Echo  to Arduino (using pin )   [blue]
  • VCC to 5V on Ardiuno [red]

Code

#include <Servo.h>
Servo cata;      // servo
const int cataPin = 2; // servo on pin 2

// degree positions for Catapult
const int launchCata = 0;   //degrees
const int resetCata  = 180; //degrees

// Ultrasonic distance sensor
// wiring...
// GND to  Ardiuno GND - black
const int trigger = 4; // green  
const int echo    = 3; // blue
// VCC signal  - red

const int sonicDelay = 100; // min of 10ms =100 microsecs
const int tooClose = 40; // 40 cm is too close

void setup() {
  // ultrasonic sensor pins for catapult
  pinMode(trigger, OUTPUT);   
  pinMode(echo, INPUT);
  cata.attach(cataPin);             
  Serial.begin(9600);
}

// sonicBat - use ultrasonic sensor to find distance to nearest object like a bat does
int sonicBat () {  
    long duration, distance; 
    digitalWrite(trigger,HIGH); 
    delayMicroseconds(sonicDelay); 
    digitalWrite(trigger,LOW);
    duration = pulseIn(echo,HIGH);  
            // pulseIn- Reads a pulse (HIGH/LOW) on a pin. 
            //   When the pin goes HIGH it starts timing 
            //   til it's Low and stops timing.
            //   Returns the length of the pulse in ms
    distance = (duration/2)/29.1; // speed of sound is: ~29.1 microsecs/cm or 1/29.1 cm/microsec
    delay(sonicDelay);
    return distance;   
}

void loop() {
  long distance;
  distance = sonicBat();
  Serial.println(distance);

  if (distance >= tooClose) {  
    cata.write(resetCata); 
    delay(10);
  }
  else { // distance < tooClose 
      cata.write(launchCata);
      delay(1600);
      cata.write(resetCata);
      delay(1600);
  }
}

Sunday, August 2, 2020

EPO Arudino (& Motors) July Summer Camp

Each Tuesday in July Electronic Parts Outlet held a 2 hour camp on Arduino. (attended).
Focus was on wiring and code and fun projects. Parts are easy to obtain in an electronics shop. 

Week 1: From Blinky to DC Motor and photo-resistor :  

Blinking LED - digitalWrite 
Dimming LED - analogWrite
DC Motor - reused LED code from above and using H Bridge to connect motor to Arduino
Photo-resistor - reuse LED wiring, but used analogRead to see how much light was sensed.

Week 2: Stepper motor Ferris wheel ...

using a push button to run.

Week 3: 2 Servo motors Popsicle stick Robot arm

A 2 axis robot arm moving around the base or move arm up and down.





Week 4: Servo motor Catapult set off by ultrasonic sensor 

Careful! If get to close the catapult will shoot a cotton ball at you!

Bring Classes together:  Obstacle avoidance

If Ultrasonic sensor found too close to object then move  Robot tank (Th class) using 2 DC motors to avoid.  
On Thursday was a Mechanical / Robotics Classes where a robot tank car using 2 DC motors had been built.  Added on top was ultrasonic sensor to measure how close to object the robot was.

Upcoming posts will cover each of these projects and more.

2 Servos motors for tiny robotic arm

Americans, at least Texans say you can do anything with duct tape and baling wire.  
Add Popsicle sticks for crafts. 
Tiny Robot Arm - good for first code tests
 [This a very simple robot arm. Google "Arduino Popsicle stick robotic arm" for more ideas.]

Servo Motor Overview:

Parts needed:

  • Arduino Uno
  • small breadboard
  • ~14 male to male breadboard jumpers
  • 2 servo motors
    • Tower pro micro servo 9G
    • servo motor small 2 sided attachment (screw to 2nd servo motor)
  • 2 potentiometers
    • any potentiometer with 3 pins that will go on a breadboard will work
    • may use 3 male to female breadboard jumpers to connect potentiometer to breadboard
  • 2 220 Ohm resistors
  • PNP (voltage changer)
    • Bridgold TIP 32C Tip32 PNP Bipolar(BJT) single Transistor, General Purpose, 100V/2A, TO-220AB
  • 1 9V battery
  • 2 half Popsicle sticks
  • electric tape
    • to secure servo motor to base  & half Popsicle sticks
  • base (screw bottom servo motor to)

Wiring



Observation:

The potentiometers do not always go from 0-1023 as shown in Arduino examplesservo > motorknob code, but the max value is specific to the voltage (3.3V or 5V) and something else unknown at moment. But it is repeatable. So the max for potentiometer is calculated in code below. In theory that code should not be needed.

Program

#include<Servo.h>

Servo bottom;
Servo robotArmMotor;

int delayT = 15;

// Can use constants like A0 and A1 for pin # or just 0 or 1. No difference, 
//    but analog pins actually start with pin # 14=A0
int pb  =A0; // or 0 analog pin 0 (A0) used to connect bottom servo to its potentiometer
int p_arm  = A1; // or 1 analog pin 1 (A1) used to connect robot arm  servo to its potentiometer

// measures max for potentiometers, but MUST rotated from min to max initially to work right!

// Set to 0 to measure max 
int pb_maxVal = 0; 
int p_arm_maxVal = 0; 

int val=0; // // variable to read the value from the analog pin
void setup() {
bottom.attach(9); // attaches the bottom servo on pin 9  to the Servo object bottom
robotArmMotor.attach(10);// attaches the robot arm servo on pin 10 to the Servo object robotArmMotor
Serial.begin(9600);
}

// Max from potentiometer is unknown, so when start move potentiometer knob to max 
//   then this code will return the max 
int potentioMax(int val, int p_maxVal) {  
    if (val > p_maxVal) p_maxVal = val;  // sets potentiometer read max
     Serial.println(val);
    Serial.println(p_arm_maxVal);
    return p_maxVal;
}

void loop() {
  val = analogRead(pb);
  pb_maxVal = potentioCalibrate(val, pb_maxVal);
  val = map(val, 0, pb_maxVal, 0, 180); // scale it by mapping 0-max to 0-180 
  robotArmMotor.write(val);               // moves the robotArmMotor servo to position
  delay(delayT);                                          // waits for the servo to get there  
    
  val = analogRead(p_arm);
  p_arm_maxVal = potentioCalibrate(val, p_arm_maxVal);
  val = map (val, 0, p_arm_maxVal, 0, 180);  // scale it by mapping 0-max to 0-180 
  robotArmMotor.write(val);                    // moves the robotArmMotor servo to position 
  delay(delayT);                        // waits for the servo to get there
                          
}

Arduino and pump

Motor Driver witn 9110 or 9110S  https://www.laskakit.cz/user/related_files/l9110_2_channel_motor_driver.pdf https://docs.sunfounder.com/pro...