Go Down

Topic: digital read and analog write (Read 143 times) previous topic - next topic


Oct 11, 2018, 10:05 am Last Edit: Oct 11, 2018, 10:24 am by sobi1sobi
Hai ,
I am new to Arduino world....
I need a small help that is i want write a code for digital input and analog output using digital read and analog write...
I am doing a small project for that i need to get input from pir sensor and give output to dcmotor in analog values..can anyone help me...please
Arduino mega2560

My. Code is
Int inputpin=2;
Int pirstate=LOW;

Void loop(



Is it correct


Is it correct
What happens when you run the program?

What PIR detector are you using? Post a link to its datasheet.
What DC motor are you using? Link, please
What motor driver are you using? Link. please

Two or three hours spent thinking and reading documentation solves most programming problems.


C++ is case-sensitive, as you have no doubt by now found out.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


Did you look at the basic examples? Because there are some very basic errors here.

Code: [Select]
int inputpin = 2;
int pirstate = LOW; //You are not using this

void setup() {
  pinMode(2, INPUT);
void loop() {
  if (digitalRead(inputpin) == HIGH)
    analogWrite(8, 0);
    analogWrite(9, 255);

  else {
    analogWrite(8, 255);
    analogWrite(9, 0);


== is the equality comparison operator, = is the assignment operator, be careful not to confuse them

Note that if you have compiler warnings turned on in your preferences and read the warnings a lot
of issues like this will be spotted automatically (although many compiler messages are rather hard
to understand at first)
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up