Voltage sharing across multiple peripherals

Hello, im trying to utilise a water level sensor and a steper motor together using one arduino uno. however, whenever i look at the ADC value output on the terminal screen, it is 0 (no water) when the steper motor is off, but fluctuates between 10-30, when the steper motor is turned on, even when there is no water. i was wondering why this was the case? additionally, whilst i was working on my breadboard, i accidentally burned the Q1 transistor (im guessing), located between the power input and USB input, is this the likely cause?

Hi, @eyasas
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.

A copy of your code and a circuit diagram would be great.
You circuit can be a hand drawn image.

Can you post a link to data/specs of the level sensor?
Do you have a DMM?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

hey tom, im a new user and cant do much with media content, but here is a picture of my breadboard configuration. the water level sensor is a basic one that comes with any arduino RFID kit, it just uses a signal from 0-5 volts. as for a DMM no, i do not have one, but i have used the UART feature to display the readings from the water level sensor.

Pictures of code is not how the forum guidelines ask that code be posted. Read the "how to use the forum" page link that @TomGeorge posted.

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <string.h>
#include <stdio.h>
#include <util/twi.h>
#include <stdlib.h>
#include <math.h>

int step_number = 0;
int motor_state = 0;
unsigned int on_off = 0;
unsigned int i = 0;
volatile int uart_time = 0;
volatile int led_blink_time = 0;
char sentence[100];
char sentence_2[100];
volatile int adc_value;

void putchar_uart(char ch) 
{
  while(!(UCSR0A & (1<<UDRE0))); 
  UDR0 = ch;                     
}

void puts_uart(char *s) 
{        
  for(i = 0; i < strlen(sentence); i++) 
  {
    putchar_uart(*(s+i));             
  }
}

void UART_init(void)
{
  UBRR0L = (103);
  UBRR0H = (0);
  UCSR0B |= (1<<3)|(1<<4);
  UCSR0C |= (1<<1)|(1<<2);
}

void ADC_init(void)
{
  ADMUX |= (1<<2)|(1<<6);
  ADCSRA |= 0b10111111;
  ADCSRA |= (1<<6);
}

void Timer0_Int(void)           
{
  TCCR0A = (1<<WGM01);          
  TCCR0B = (1<<CS00)|(1<<CS01); 
  TIMSK0 = (1<<OCIE0A)|(1<<OCIE0B);         
  OCR0A = 250-1;                
}

void stepper_process_clockwise(void)
{
    switch(step_number)
      {
        case 0:
        PORTB |= (1<<4);
        PORTB &= ~((1<<1)|(1<<2)|(1<<3));
        break;

        case 1:
        PORTB |= ((1<<4)|(1<<3));
        PORTB &= ~((1<<1)|(1<<2));
        break;

        case 2:
        PORTB |= (1<<3);
        PORTB &= ~((1<<1)|(1<<2)|(1<<4));
        break;

        case 3:
        PORTB |= ((1<<2)|(1<<3));
        PORTB &= ~((1<<1)|(1<<4));
        break;

        case 4:
        PORTB |= (1<<2);
        PORTB &= ~((1<<1)|(1<<3)|(1<<4));
        break;

        case 5:
        PORTB |= ((1<<2)|(1<<1));
        PORTB &= ~((1<<3)|(1<<4));
        break;

        case 6:
        PORTB |= (1<<1);
        PORTB &= ~((1<<2)|(1<<3)|(1<<4));
        break;

        case 7:
        PORTB |= ((1<<1)|(1<<4));
        PORTB &= ~((1<<2)|(1<<3));
        break;

        default:
        PORTB &= ~((1<<1)|(1<<2)|(1<<3)|(1<<4));
        break;
      }

      step_number++;

      if(step_number > 7)
      {
        step_number = 0;
      }
}

void on_off_button(void)
{
  if((((PIND >> 5) & 1) == 0))
    {
      _delay_ms(50);
      if((((PIND >> 5) & 1) != 0))
      {
        on_off ^= 1;
      }
    }
}

ISR(TIMER0_COMPA_vect)
{
  uart_time++;

  if(on_off == 1)
  {
    stepper_process_clockwise();
  }
  
  if(on_off == 0)
  {
    PORTB &= ~((1<<1)|(1<<2)|(1<<3)|(1<<4));
  }
}

ISR(TIMER0_COMPB_vect)
{
  led_blink_time++;

  if(adc_value < 490 && led_blink_time > 500)
  {
    PORTD &= ~((1<<3)|(1<<4));
    PORTD ^= (1<<2);
    led_blink_time = 0;
  }

  if(adc_value < 540 && adc_value > 490 && led_blink_time > 500)
  {
  PORTD |= (1<<2);
  PORTD &= ~(1<<4);
  PORTD ^= (1<<3);
  led_blink_time = 0;
  }

  if(adc_value < 580 && adc_value > 540 && led_blink_time > 500)
  {
    PORTD |= (1<<2)|(1<<3);
    PORTD ^= (1<<4);
    led_blink_time = 0;
  }

  if(adc_value > 600)
  {
    PORTD |= (1<<2)|(1<<3)|(1<<4);
  }
}

ISR(ADC_vect)  
{
  adc_value = ADC;
}

int main(void)
{
  DDRB |= (1<<1)|(1<<2)|(1<<3)|(1<<4);
  DDRD |= (1<<2)|(1<<3)|(1<<4);

  UART_init();
  ADC_init();
  Timer0_Int();
  sei();

  while(1)
  {
    on_off_button();

    if(uart_time > 250)
    {
      sprintf(sentence, "Water level is: %d\n", adc_value);
      puts_uart(sentence);
      uart_time = 0;
    }
  }
}

and here is the datasheet for the arduino and steper motor

http://eeshop.unl.edu/pdf/Stepper+Driver.pdf

You are powering the motor and the sensor through the same wires. In particular the red and black wires from the Arduino to the breadboard are carrying current for both. Of most concern is the black wire, the current through it to the stepper motor will cause voltage across the wire and the contacts, with the result that the 0V reference the sensor uses isn't really 0V. This voltage will show up on the A2D readings.

You should not be powering a motor from an Arduino, the motor should have it's own supply. Don't forget to include a common ground connection.

hello Perry, thank you for the response, in regards to the 5v and ground connections, would using the other 5v and ground pins available on the arduino fix this problem? or would it still act the same way?

At least he is not attempting to use "Vin" or the "barrel jack"! :worried:

I'd be firstly concerned with the "burning of Q1". What does "burned the Q1" transistor mean?

As you can see from the image Q1 is very important to the start of the Uno. Q1, being number one, represents a primary beginning point for garbage in garbage out.

I have to say I find questions like that a bit frustrating :thinking:

How about trying it and letting us know what you find?

burned as in, it turned red, and smoke started coming out from that part of the board.

Typically, after the parts of a Uno development board get red hot and produce smoke the code does not work properly.

oh I see. VS code still uploads properly, and the circuit is doing it's job, but i'll keep an eye out on anything else that comes up. thanks for the help.

But if it is just the regulator and you simply remove it, then things may be OK. :+1:

hey Perry, using the other pins fixes the problem, but I just rewired the breadboard to make it easier like you adviced. thanks for the help.

1 Like

Good. Do you understand why? There is an important lesson for you in there somewhere.

im guessing because of the difference between a parallel and series circuit? however, im not sure i understand what common ground connections are needed.

Do you understand Ohm's Law ?

In your original circuit the motor current passed through the same black wire as was used for the sensor ground. Wires have resistance. You pass current through them and you get voltage across them according to Ohm's law. The voltage at the breadboard end of the black wire is not the same as the ground voltage at the Arduino. That voltage gets fed into the sensor via its ground connection, and shows up at its output.