The frequency of the pulses range from 1-10000 Hz and voltage ranges from 1-5 volt. I have tried the following code, its working for digital pins but not for analog.Do you guys have any solution for it.
int pin = A0;
volatile unsigned int pulse = 0;
void setup()
{
Serial.begin(9600);
pinMode(pin, INPUT_PULLUP);
attachInterrupt(0, count_pulse, RISING);
}
void loop()
{
pulse=0;
interrupts();
delay(100);
noInterrupts();
Serial.print("Pulses per second: ");
Serial.println(pulse);
}
void count_pulse()
{
pulse++;
}
Different Arduino boards (Mega, Uno, Nano etc) are different in terms of which pins work with interrupts. You need to read the documentation for your board, or much better, use #ifdef to adapt to any board:
#ifdef __AVR_ATmega32U4__ // Leonardo has different int mapping
#ifdef __SAM3X8E__ // Due processor has different timer setup
#ifdef __AVR_ATmega328P__
Check out the documentation on arduino.cc/reference.
In short if you are using the arduino uno you can only use the digital pin #2 and #3 for attachinterrupt() operation... https://youtu.be/srr0TTRPPKU
Related
I'm writing a simple project, using NodeMCU as my board. I have 2 analog devices: moisture sensor and brightness sensor. Since NodeMCU has only one analog pin, I try to power them in turns. To do so, I connect them to digital pins of NodeMCU. Digital pins output 3.3V 20-40mA in HIGH state (checked that with multimeter). That must be enough to power those devices. In the LOW state, according to Arduino IDE docs, voltage must be 0V (and that also was checked) and the sensor must not receive any current and, thus, must not output anything to A0. However, at the end I get correlated results: if I flash light at the photoresistor, humidity data is also affected. And vice-versa. How can I avoid it?
Here's the code, I'm using to do, what I described:
const int analogInputPin = A0; // Analog 0 (A0) on the board
// Several devices will use the same analog pin to send data
const int lightSensorResultPin = analogInputPin;
const int moistureSensorResultPin = analogInputPin;
const int lightSensorPowerPin = 16;
int lightSensorResult = 0;
const int moistureSensorPowerPin = 5;
int moistureSensorResult = 0;
void setup() {
Serial.begin(9600);
pinMode(lightSensorResultPin, INPUT);
pinMode(lightSensorPowerPin, OUTPUT);
digitalWrite(lightSensorPowerPin, LOW);
pinMode(moistureSensorResultPin, INPUT);
pinMode(moistureSensorPowerPin, OUTPUT);
digitalWrite(moistureSensorPowerPin, LOW);
}
void loop() {
/*==LIGHT SENSOR DATA READ BLOCK==*/
digitalWrite(lightSensorPowerPin, HIGH);
delay(1000);
lightSensorResult = analogRead(lightSensorResultPin);
digitalWrite(lightSensorPowerPin, LOW);
delay(1000);
/*==END OF LIGHT SENSOR DATA READ BLOCK==*/
/*==MOISTURE SENSOR DATA READ BLOCK==*/
digitalWrite(moistureSensorPowerPin, HIGH);
delay(1000);
moistureSensorResult = analogRead(moistureSensorResultPin);
digitalWrite(moistureSensorPowerPin, LOW);
delay(1000);
/*==END OF MOISTURE SENSOR DATA READ BLOCK==*/
Serial.print("Value on the light sensor: ");
Serial.println(lightSensorResult); // Light value. Low for bright
Serial.print("Value on the moisture sensor: ");
Serial.println(moistureSensorResult); // Moisture value. Low for wet
}
UPD: Here's the schematic
Instead of using digital outputs to switch on and off the different sensors, but combining their analog outputs, I would use an analog switch like 4066 to select which sensor you want to measure.
I am trying to get my code to show a switch input from a joystick on an Arduino Uno.
When I view it on the serial monitor, I see the x and y axis being displayed when I move the joystick.
When I click it I do not get anything to show up. I have the switch set to pin 2 on the Arduino.
There are no errors when I compile the code.
int pin_x = A0;
int position_x = 0;
int pin_y = A1;
int position_y = 0;
int pin_z = 2; // switch pin
int position_z = LOW;
void setup() {
Serial.begin(9600); // initialize serial communications at 9600 bps
pinMode(pin_x, INPUT); // set pin mod as INPUT
pinMode(pin_y, INPUT);
pinMode(pin_z, INPUT); // switch pin
}
void read(){
position_x = analogRead(pin_x);
position_y = analogRead(pin_y);
position_z = digitalRead(pin_z);
}
void show(){
Serial.print(" X:"); //print information to Serial Monitor
Serial.print(position_x);
Serial.print(" Y:");
Serial.print(position_y);
Serial.print(" Z:");
Serial.print(position_z);
}
void loop() {
read();
show();
delay(500);
}
There are several methods you could use to find the error, here are a couple suggestions:
read the datasheet of your joystick
use a multimeter to check conductivity of the two switch pins
hook something else to your switch input, check that the input is working
That said, the way your code is written it expects your switch pin to be connected to Vcc, check if that is really the case.
Try this:
void setup() {
Serial.begin(9600); // initialize serial communications at 9600 bps
pinMode(pin_x, INPUT); // set pin mod as INPUT
pinMode(pin_y, INPUT);
pinMode(pin_z, INPUT); // switch pin
digitalWrite(pin_z, HIGH);
}
Otherwise code looks correct. The switch on a Joystick is indeed digital, while the others are analog, you got that right.
Here's a video on how to work with joysticks, maybe it helps: https://www.youtube.com/watch?v=MlDi0vO9Evg
I an getting a constant HIGH from 'inputPintwo' on the serial monitor. When 'inputPin' goes HIGH the relay is triggered and works properly because 'inputPintwo' is also HIGH (all the time).
I have a Very similar setup to: 2 PIR motion sensors +Arduino
I am not using pin 0 or 1 like the above answered question. I have replaced the sensor with a different one, in case it was bad hardware. I also unplugged the sensor and it still reads HIGH. The jumper is on retriggering on both sensors.
int ledPin = 13;
int inputPin = 2;
int inputPintwo = 4;
int pirState = LOW;
int val = 0;
int valtwo = 0;
#define RELAY1 7
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
pinMode(inputPintwo, INPUT);
pinMode(RELAY1, OUTPUT);
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin);
valtwo = digitalRead(inputPintwo);
if (val == HIGH && valtwo == HIGH) {
digitalWrite(ledPin, HIGH);
if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
Serial.println("Light ON");
digitalWrite(RELAY1,1);
delay(500);
digitalWrite(RELAY1,0);
delay(500);
digitalWrite(RELAY1,1);
delay(500);
digitalWrite(RELAY1,0);
delay(500);
digitalWrite(RELAY1,1);
}
}
else {
digitalWrite(ledPin, LOW);
if (pirState == HIGH){
Serial.println("Motion ended!");
digitalWrite(RELAY1,0);
pirState = LOW;
Serial.println("Light OFF");
}
}
}
I expect both sensors to go HIGH only when motion is detected, which will cause the relay to go on and off several times, then stay on until the timer runs out on the sensors.
To identify the problem I recommend you to start with checking the hardware. You will need voltmeter/multimeter.
Double check if you are interfacing the sensor properly (check datasheet). Didn't you forget to connect e.g. the pull-down resistors?
Check power supply voltage on sensors – is the voltage within
manufacturer specifications?
Check breadboard connections if you are using one.
Check sensor output behaviour (voltage), if there is or is not a movement. Is the voltage constant or not? Constant voltage means that PIR sensor is NOT working properly. Before performing of this test disconnect output from Arduino input.
If everything seems OK or you do not have voltmeter, try to disconnect the PIR sensor and connect a wire between Arduino pin 4 and ground. Does digitalRead(inputPintwo) return LOW? If yes, you know that reading of the pin state works fine.
Below please see some recommendations related to your code:
use #define directive or static const int variable type to define Arduino pins as you do it with relay output pin RELAY1.
Example:
#define LED_PIN 13
#define INPUT_PIN 2
#define INPUT_PINTWO 4
or
static const int ledPin = 13;
static const int inputPin = 2;
static const int inputPintwo = 4;
In your case, where you are only interested in digital value (LOW/HIGH), use built pull-up resistor on the input pins. Thus the log. voltage level on the floating input pin is defined (HIGH). If you do not use pull-up resistors, voltage can be either log. 0 (LOW) or log. 1 (HIGH), what can lead to strange program/state machine behaviour
To activate pull-up resistors in the input pins, use
pinMode(inputPin, INPUT_PULLUP);
pinMode(inputPintwo, INPUT_PULLUP);
I'm trying to read voltage value using Analog pin A0 in arduino uno and transmit the read voltage to Nodemcu but not getting same voltage at NodeMcu as on Arduino side for Ex. for 5 volt at Arduino i get only 4 volt at Nodemcu.
i have made the delay of both the sketches equal even tried without any delay
also tried connecting the ground pin of both device
ARDUINO CODE
#include <SoftwareSerial.h>
SoftwareSerial s(5,6);
void setup() {
s.begin(9600);
Serial.begin(9600);
}
void loop() {
// read the input on analog pin 0:
int ADCdata = analogRead(A0);
float voltage = (ADCdata * 0.0048828125);
Serial.println(ADCdata);
Serial.println(voltage);
if(s.available()>0)
{
s.write(voltage);
}
delay(1000);
}
NODEMCU CODE
#include <SoftwareSerial.h>
SoftwareSerial s(D6,D5);
void setup() {
s.begin(9600);
Serial.begin(9600);
}
void loop() {
s.write("s");
if (s.available()>0)
{
data=s.read();
Serial.println(data);
}
delay(1000);
}
I would send the float data as a string:
s.println(value)
This will append a newline to mark the end of the string.
On the receiving side, read the line and convert to float.
float value = s.parseFloat();
I'm writing code to run on an ATtiny being programmed by an Arduino as ISP. The ATtiny is to send AT commands over serial link to an RN42 Bluetooth module.
Since the ATtiny has no UART I'm using SoftwareSerial on pins 0 and 1. It seemed logical to put Tx on the "Data Out"/MISO pin and Rx on the "Data In"/MOSI pin. The documentation says to declare this like SoftwareSerial mySerial(Rx, Tx); but I found it only works if you declare it the other way round like SoftwareSerial mySerial(Tx, Rx);
I've taken a screenshot of my code and the pinout, I feel like I'm missing something but when I do it like this it works and makes the Bluetooth module enter command mode. Is the documentation the wrong way round?
Code and Pinout
I realised the error of my ways, I was unnecessarily setting the pinMode of the Rx and Tx pins. This threw me off as I thought setting the Rx pin to OUTPUT wouldn't work when actually it does, so I was outputting data on my Rx line and receiving it on the Tx line! The answer is to not assign direction and just let SoftwareSerial handle the pins. Pass the parameters in the order (Rx, Tx).
Here is my cleaner code that is working much better:
#include <SoftwareSerial.h>
const int Rx = 0; // pin 5 on ATtiny - DI/MOSI
const int Tx = 1; // pin 6 on ATtiny - DO/MISO
const int ButtonIn = 2;
const int OK_LED = 4;
int buttonState = 0;
SoftwareSerial serialBT(Rx, Tx);
void setup()
{
pinMode(ButtonIn, INPUT);
pinMode(OK_LED, OUTPUT);
serialBT.begin(9600);
}
void loop()
{
buttonState = digitalRead(ButtonIn);
if (buttonState == 0)
{
serialBT.print("$"); // $$$ enters RN42 command mode
serialBT.print("$");
serialBT.print("$");
delay(3000);
serialBT.println("R,1");
digitalWrite(OK_LED, HIGH);
delay(5000);
digitalWrite(OK_LED, LOW);
}
}